this code doesn't compile, it says improper use of typedef 'hello' in function main()Code:#include <iostream.h> class hello{ private: int a; public: static int val; }; void main(){ cout<<"gaurav "<<hello.val; }
i am using borland c++ compiler
this code doesn't compile, it says improper use of typedef 'hello' in function main()Code:#include <iostream.h> class hello{ private: int a; public: static int val; }; void main(){ cout<<"gaurav "<<hello.val; }
i am using borland c++ compiler
Code:#include <iostream> class hello{ private: int a; public: static int val; }; int hello::val; int main(){ std::cout<<"gaurav "<<hello.val; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
You must use the scope resolution operator (::) instead of the dot operator, which is used to reference members of an object.Also, you must declare static member variables as previously mentioned.Code:cout << "gaurav " << hello::val;
Try it this way.
KurtCode:#include <iostream> // new header using namespace std; class hello{ private: int a; public: static int val; }; int hello::val = 12; // define an initialize it int main(){ // main must return int cout<<"gaurav "<< hello::val; // scope resolution op needed }
Both the dot operator and the scope resolution operator work for static members with VC++6. But if you don't initialize the static member, then you get errors:You must use the scope resolution operator (::) instead of the dot operator, which is used to reference members of an object.
Code:int hello:val; //intializes to 0 int hello::val = 12;
Last edited by 7stud; 02-24-2006 at 02:06 PM.