find a bug in following code :
class A
{
public:
static int i;
void print()
{
cout<< i << endl ;
}
};
int main()
{
A a;
a.print();
}
I run above code, and I am getting "undefined reference to `A::i'" . Why I am getting this error ?
Since A::i
is a static
member, it has to be defined outside of the class:
using namespace std;
class A
{
public:
static int i; // A::i is declared here.
void print()
{
cout << i << endl;
}
};
int A::i = 42; // A::i is defined here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With