If i try to initialize obj_s
it asks me to make it const
- and i cant do that for i have to keep count of my Created Objects.
#include<iostream>
class A
{
static int obj_s=0;
public:
A(){ ++obj_s;co开发者_高级运维ut << A::obj_s << "\nObject(s) Created\n"; }
};
int main()
{
A a,b,c,d;
}
The code below keeps giving me the following error:
[Linker error] undefined reference to `A::obj_s'
[Solved]
The code is giving the error because the object is not getting created in the second case, and in the first its not initializing, the way its supposed to - Here's the fixed Code:
#include<iostream>
class A
{
static int obj_s;
public:
A()
{ obj_s++; std::cout << A::obj_s << "\nObject(s) Created\n" ; }
};
int A::obj_s=0; // This is how you intialize it
int main()
{
A a ,b,c,d;
}
精彩评论