I was learning static data members in c++ and I wrote this code but on compilation, it is giving me this error:
main.cpp:21:12: error: ‘int Test1::a’ is private within this context
Test1::a=50;
^
main.cpp:17:5: note: declared private here int Test1::a;
But I don't know why this error is coming and how to solve this.
#include <iostream>
using namespace std;
class Test1{
static int a; //declaration
};
int Test1::a; // definition
int main()
{
Test1::a=50;
cout<<"a= "<<Test1::a<<endl;
}
static
is a red herring. Unless specified explicitly, everything in aclass
isprivate
by default.static
has nothing to do with it.