Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using static variables

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm trying to declare a static variable in a class, which any subclass can access. For example,

class superclass
{
public:
static int i;

};

class subclass1: public superclass
{
void main()
{
i++;
}
}

Now if in my main program, I do something like:

subclass1* S;
S = new subclass1;
S -> main();

I get an unresolved external symbol error:

"error LNK2001: unresolved external symbol "public: static int statictester::i""

What does this error mean, and any suggestions on how I can fix this?

Bob
 
In addition to what you've done, you should also declare the static variable from outside of the class:

int superclass::i =0;
 
I'd much rather initialize it from within the class constructor. Is there any way I can do this?

Bob
 
No. You cann't do that.
static data member must be initialized at file scope instead of the class scope.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top