Thread: Static member data inaccessible

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Static member data inaccessible

    Hi, I'm making a little Chess program for my first C++ project. I've done a whole lot of programming before, so don't worry.

    OK, I have two static variables of the ChessPiece class:
    Code:
    static uShort whitePieces;
    static uShort blackPieces;
    And I'm trying to access them in the constructor:
    Code:
       if (side == White)
       {
          whitePieces++;
       }
       else
       {
          blackPieces++;
       }
    For some reason, my DJGPP gcc compiler (used through RHIDE, my IDE) returns:

    undefined reference to 'ChessPiece::whitePieces'
    undefined reference to 'ChessPiece::blackPieces'

    Oh, and yes I have tried this:
    Code:
       if (side == White)
       {
          ChessPiece::whitePieces++;
       }
       else
       {
          ChessPiece::blackPieces++;
       }
    Same problem. Can anyone solve this?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Can post the entire code file, well at least the full implemintation of ChessPiece class?
    Do you have the constructor declaired in the class before the vars?

    ie
    class ChessPiece
    {
    public:
    ChessPiece()
    {
    whitePiece++;
    }

    private:

    static int whitePiece;

    }; //end class ChessPiece

    If your code looks like that, put the private/protected section before the public section

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    "Some C++ compilers originally allowed static member variables to be automatically declared while others required an external declaration, due to conflict the standard now strictly states that any static members must be redeclared outside the class." - C++ Standard Reference

    This was a problem for me once, here is an example of how to use statics inside a class

    Code:
    PHP Code:
    class example{      private:          static int i_Ai_B;          /*Other stuff*/      public:          example();          /*Other Stuff*/ }; /*static?*/ int example::i_Aexample::i_B
    After you close your class definition with }; you need to redeclare the variables as i have done (i forget if the keyword static should be there so i have commented it out, test it both ways and see which one works, i feel like it probably isn't supposed to be there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. compilation problems with static data member
    By viral612 in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2008, 06:55 AM
  3. 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
  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