Undefined reference

Discussion in 'C' started by state, Aug 14, 2012.

  1. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
    state likes this.
  3. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Thank You
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice