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

This is a discussion on How to initialize a private static const array of a private child class in a class? within the C++ Programming forums, part of the General Programming Boards category; I've been working on my homework project and now I am to declare a class: Code: #define PIECE_SIZE 5 #define ...

  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
    21,414
    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.
    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

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, 11:54 AM

Tags for this Thread


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