Thread: Can not access static data member.

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Can not access static data member.

    Here is the code:
    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Person {
    private:
    	static const int _age = 20;
    	static vector<int> _elems  ;
    		
    public:
    	static void getAge();
    	
    };
    
    void Person::getAge() {
    	cout << _age << endl;
                    //error occure here, _elems.size() can not be resolved.
    	cout << _elems.size() << endl;
    }
    
    int main() {
    	Person p;
    	p.getAge();
    	
    	return 0;
    }
    The compiler error in Windows is: error LNK2019: unresolved external symbol,
    Can any help me why data member "_age" can be accessed but "_elems" failed.
    Thanks.

  2. #2
    check out:
    http://msdn.microsoft.com/library/de...m/class_26.asp

    Static member functions have external linkage. These functions do not have this pointers. As a result, the following restrictions apply to such functions:

    * They cannot access nonstatic class member data using the member-selection operators (. or >).
    * They cannot be declared as virtual.
    * They cannot have the same name as a nonstatic function that has the same argument types.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Your problem is that although you have declared the static member _elems, you haven't defined it. Add the statement below after your class definition in order to define _elems:

    Code:
    vector<int> Person::_elems;
    All non-const static data members of a class need to be defined in this manner. Incidently, you shouldn't use identifier names that begin with an underscore as these are reserved for use by the implementation.

  4. #4
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    You can not initialise static members inside class....infact you cannot initilise anything inside class decleration like you have done...its only done in constructor....

    as far as your code is concern...you should do something like this

    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Person {
    private:
    	static const int _age ;
    	static vector<int> _elems  ;
    		
    public:
    	static void getAge();
    	
    };
    
    const int Person::_age=0;
    void Person::getAge() {
    	cout << _age << endl;
                    //error occure here, _elems.size() can not be resolved.
    	cout << _elems.size() << endl;
    }
    
    int main() {
    	Person p;
    	p.getAge();
    	
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> infact you cannot initilise anything inside class decleration like you have done

    Sure you can. static const int _age = 20; is perfectly legal according to the standard, although I know of at least one old compiler that doesn't allow it.

    >> Incidently, you shouldn't use identifier names that begin with an underscore as these are reserved for use by the implementation.

    Actually, names that begin with an underscore followed by a capital letter are reserved for the implementation, so these names are legal, although some people suggest never using leading underscores to avoid confusion.

  6. #6
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by Daved
    Sure you can. static const int _age = 20; is perfectly legal according to the standard, although I know of at least one old compiler that doesn't allow it.
    I didn't knew that(just tried with VC 6.0 which doesn't allow it).....thanx for the correction......Its working with DEV C++.....i guess latest versions of VC++ does support that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Replies: 2
    Last Post: 09-29-2001, 03:36 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM