Code: ............................. class BookingDetails:public Tourist { static std::string tourist_name[5]; static int room_id[5],booking_id[5]; static float rent[5]; static int counter; public: static void storevalues(Tourist &bd) { tourist_name[counter]=bd.name; room_id[counter]=bd.id; booking_id[counter]=bd.bookingid[counter]; rent[counter]=bd.rent; counter++; } ...................................................................................... This is a snippet from a Hotel Management System program.I have initialized all the static variables except names.When I try to initialize it outside the class it gives "constructor must be used" error.I cannot use a constructor because it is incorrect to initialize static variables using a constructor.Otherwise it is throwing an undefined reference error.I am using DEVC++. Thank You for your help
I don't get the same error in Visual Studio 2010: Code: class statictest { static int defaultint; public: static int publicint; private: static int privateint; //friend void test50(); }; int statictest::defaultint; int statictest::publicint; int statictest::privateint; #define FUNC test50 void test50() { statictest it; it.defaultint=3; it.publicint=4; it.privateint=5; printf("%d %d %d\n",it.defaultint,it.publicint,it.privateint); } Errors: 1>c:\users\dave\documents\visual studio 2010\projects\cack\cack.cpp(37): error C2248: 'statictest::defaultint' : cannot access private member declared in class 'statictest' 1> c:\users\dave\documents\visual studio 2010\projects\cack\cack.cpp(17) : see declaration of 'statictest::defaultint' 1> c:\users\dave\documents\visual studio 2010\projects\cack\cack.cpp(16) : see declaration of 'statictest' 1>c:\users\dave\documents\visual studio 2010\projects\cack\cack.cpp(39): error C2248: 'statictest::privateint' : cannot access private member declared in class 'statictest' 1> c:\users\dave\documents\visual studio 2010\projects\cack\cack.cpp(23) : see declaration of 'statictest::privateint' 1> c:\users\dave\documents\visual studio 2010\projects\cack\cack.cpp(16) : see declaration of 'statictest' If I uncomment and the friend definition then this compiles and runs without error. Don't forget class members are private by default so tourist_name and the other 4 will need initialising by a friend function (or a class static function). If I uncomment the three "int statictest::..." lines then I get errors that are probably equivalent to your undefined references: 1>Cack.obj : error LNK2001: unresolved external symbol "private: static int statictest::privateint" (?privateint@statictest@@0HA) 1>Cack.obj : error LNK2001: unresolved external symbol "public: static int statictest::publicint" (?publicint@statictest@@2HA) 1>Cack.obj : error LNK2001: unresolved external symbol "private: static int statictest::defaultint" (?defaultint@statictest@@0HA) At a guess you probably need to go back and RTFM on how static class variables work. Are you sure you definitely need them to be static? Static class members are independent from any specific instance of that class; they are effectively global variables available within class scope to all objects of that class, and given the names of your variables it seems that static is exactly what you *don't* want. I would have thought that each BookingDetails object would need its own tourist name, booking id, room id etc.