static class members

This is a discussion on static class members within the C++ Programming forums, part of the General Programming Boards category; My compiler gives me this error message when I try to build the project -- Code: 1>Aisle.obj : error LNK2001: ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    static class members

    My compiler gives me this error message when I try to build the project --
    Code:
    1>Aisle.obj : error LNK2001: unresolved external symbol "private: static int Aisle::numberOfAisles" (?numberOfAisles@Aisle@@0HA)
    1>C:\Users\Sterling\Documents\Visual Studio 2008\Projects\Gas Station\Debug\Gas Station.exe : fatal error LNK1120: 1 unresolved externals
    The code for the Aisle class is

    Code:
    class Aisle
    {
    public:
    	Aisle(void);
    	~Aisle(void);
    
    	void setNumber(int);
    	int getNumber();
    	static int getNumberOfAisles();
    private:
    	int number;
    	static int numberOfAisles;
    };
    Code:
    #include "Aisle.h"
    
    Aisle::Aisle(void)
    {}
    
    Aisle::~Aisle(void)
    {}
    
    void Aisle::setNumber(int aNum) {
    	number = aNum;
    }
    
    int Aisle::getNumber() {
    	return number;
    }
    
    int Aisle::getNumberOfAisles() {
    	return numberOfAisles;
    }
    What am I doing wrong with numberOfAisles? I declare it as a private static int, then make a static function to return it. I don't see why there is a problem.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,101
    The problem is that you did not define it. You should define the static member variable in the source file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You have to define it. Since it is static, there is only one instance of it no matter how many class instances are created. A normal class member variable is constructed when the class object is constructed, but where is the single static variable constructed? You have to define it (and initialize it if desired) for the compiler.

    It's easy to do, just add this to your cpp file:
    Code:
    int Aisle::numberOfAisles = 0; // or whatever initialization you want

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Whenever I create a new instance of an Aisle, will

    int Aisle::numberOfAisles = 0;

    set numberOfAisles to zero each time?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by SterlingM View Post
    Whenever I create a new instance of an Aisle, will

    int Aisle::numberOfAisles = 0;

    set numberOfAisles to zero each time?
    No. It is just like a global initializer. It only happens once.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 05:45 AM
  2. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  4. Replies: 10
    Last Post: 07-26-2008, 09:44 AM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21