Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: error: invalid use of qualified-name

Update: I think it's fixed. Thanks guys!

I'm getting an error, and I just can't figure it out. I have this code:

//A Structure with one variable and a constructor
struct Structure{
  public:
    int dataMember;

    Structure(int param);
};

//Implemented constructor
Structure::Structure(int param){
    dataMember = param;
}

//A class (which I don't plan to instantiate), 
//that has a static object made from Structure inside of it
class unInstantiatedClass{
  public:   
    static Structure structObject;
};

//main(), where I try to put the implementation of the Structure from my class,
//by calling its constructor
int main(){
    //error on the line below
    Structure unInstantiatedClass::structObject(5);

    return 0;
}

On the line that says "Structure unInstantiatedClass::structObject(5);", I get an error that reads:

error: invalid use of qualified-name 'unInstantiatedClass::structObject'

I've googled this error and looked through several forum posts, but everybody's problems seem to be different. I've also tried googling "static struct object inside class" and other related phrases, but haven't found any that I think really relate to my problem.

What I'm trying to do here is: Have a class which I don't ever instantiate. and Have, inside that class, a static object, which has a variable that can be set via constructor.

Any help is appreciated. Thanks.

like image 270
David Vaughan Avatar asked Dec 03 '25 05:12

David Vaughan


1 Answers

The definition of the static member can't be inside a function:

class unInstantiatedClass{
  public:   
    static Structure structObject;
};

Structure unInstantiatedClass::structObject(5);

int main() {
    // Do whatever the program should do
}
like image 176
Mike Seymour Avatar answered Dec 04 '25 21:12

Mike Seymour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!