Thread: static class members

  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
    28,413
    The problem is that you did not define it. You should define the static member variable in the source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 01:22 PM
  4. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM