Thread: How to initialize a private static const array of a private child class in a class?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Question How to initialize a private static const array of a private child class in a class?

    I've been working on my homework project and now I am to declare a class:
    Code:
    #define PIECE_SIZE 5
    #define PIECE_NUM 2
    #include <vector>
    class Piece
    {
    	class InternalPiece
    	{
    		std::vector<bool> Array;
    		
    		public:
    		InternalPiece(const bool a[])
    		{
    			for (int i = 0; i < PIECE_SIZE * PIECE_SIZE; ++i)
    				Array[i] = a[i];
    		}
    		bool operator()(const int &x, const int &y) const
    		{
    			return (bool)Array[x * PIECE_SIZE + y];
    		}
    	};
    	public:
    	static const InternalPiece pieces[];
    And now how am I to initialize the array `pieces'? Initialize it within the class definition is unavailable:
    Code:
    error: expected constructor, destructor, or type conversion before ‘=’ token
    And so is to initialize when you have a Piece object, since the class InternalPiece is private:
    Code:
    error: ‘class Piece::InternalPiece’ is private
    Anyone knows how to solve this? Great thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Code:
    //declaration
    static const InternalPiece pieces[];
    
    
    //definition in global scope
    const Piece::InternalPiece Piece::pieces[N] = {true, true};
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    This doesn't work since:
    1. InternalPiece variables are constructed from a bool array.
    2. The class InternalPiece is private hence we cannot call the constructor outside the class Piece definition.


    Quote Originally Posted by jinhao View Post
    Code:
    //declaration
    static const InternalPiece pieces[];
    
    
    //definition in global scope
    const Piece::InternalPiece Piece::pieces[N] = {true, true};

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since InternalPiece is private, why is it being exposed to the public via the pieces member variable? This consideration aside, I can suggest that you provide a default constructor, and then write in a source file:
    Code:
    const Piece::InternalPiece Piece::pieces[PIECE_NUM];
    Incidentally, change PIECE_SIZE and PIECE_NUM from macros to static const integer member variables of Piece, or possibly an enum in Piece.

    Quote Originally Posted by ryanli
    # The class InternalPiece is private hence we cannot call the constructor outside the class Piece definition.
    That is true, but by correctly qualifying the static member's name, one can initialise it outside of the definition of the class.
    Last edited by laserlight; 04-10-2009 at 02:22 AM.
    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

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. assignment operator for static array inside a class
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2008, 11:11 AM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

Tags for this Thread