Trouble Declaring 3D Array in Header File

Discussion in 'C++' started by free2klim, Jul 21, 2006.

  1. free2klim

    free2klim Guest

    Hi,
    I am relatively new to programming C++ and I am writing a small program
    in which I am using a 3D array of type int. The 3D array is a member
    of class GameBoard, which I have defined in the GameBoard.h header file
    as follows:

    class GameBoard {

    //function prototypes
    public:
    GameBoard();
    int getMovePeg();
    void readMovePeg();
    void readDestPeg();
    void makeMove();
    bool gameOverCheck();
    void endGame();
    void displayBoard();
    private:
    bool peg;
    int movePeg, destPeg, jumpPeg, movesTaken, leftPegs;

    //HERE IS THE DECLARATION FOR THE ARRAY
    int holes[15][5][2];

    void initHoles();
    string printHole(int);
    };

    I then
    #include "GameBoard.h"
    in the GameBoard.cpp file. Then I try to do this with the array:

    GameBoard::holes = {{{0,0},{1,3},{2,5},{0,0},{0,0}}, //hole 0

    {{0,0},{3,6},{4,8},{0,0},{0,0}}, //hole 1
    {{0,0},{4,7},{5,9},{0,0},{0,0}}, //hole 2
    {{0,0},{1,0},{4,5},{6,10},{7,12}}, //hole 3
    {{0,0},{7,11},{8,13},{0,0},{0,0}}, //hole 4
    {{0,0},{2,0},{4,3},{8,12},{9,14}}, //hole 5
    {{0,0},{3,1},{7,8},{0,0},{0,0}}, //hole 6
    {{0,0},{4,2},{8,9},{0,0},{0,0}}, //hole 7
    {{0,0},{4,1},{7,6},{0,0},{0,0}}, //hole 8
    {{0,0},{5,2},{8,7},{0,0},{0,0}}, //hole 9
    {{0,0},{6,3},{11,12},{0,0},{0,0}}, //hole 10
    {{0,0},{7,4},{12,13},{0,0},{0,0}}, //hole 11
    {{0,0},{7,3},{8,5},{11,10},{13,14}}, //hole 12
    {{0,0},{8,4},{12,11},{0,0},{0,0}}, //hole 13
    {{0,0},{9,5},{13,12},{0,0},{0,0}}}; //hole 14

    I get this error:

    "error C2761: 'int GameBoard::holes[15][5][2]' : member function
    redeclaration not allowed"

    What am I doing wrong? I have tried everything I could think of to get
    this array to work. Thanks in advance for any help that anyone can
    offer! If I need to provide any more code or explanation, please let
    me know.

    Brian
     
    free2klim, Jul 21, 2006
    #1
    1. Advertising

  2. free2klim

    Guest

    free2klim wrote:
    > Hi,

    y:
    >
    > int GameBoard::holes = {{{0,0},{1,3},{2,5},{0,0},{0,0}},


    ^I guess you forgot the "int" before the GameBoard there?

    > "error C2761: 'int GameBoard::holes[15][5][2]' : member function
    > redeclaration not allowed"


    g++ often has more insightful error messages:

    ref.cc:27: error: `int GameBoard::holes' is not a static member of
    `class GameBoard'

    Adding static to the declaration within the class (.h), g++ reports:

    ref.cc:27: error: conflicting declaration 'int GameBoard::holes'
    ref.cc:20: error: 'GameBoard::holes' has a previous declaration as `int
    GameBoard::holes[15][5][2]'

    So, the types mismatch, we'll have to add the [][][] to the declaration
    too.
    I ended up with:

    class GameBoard{
    ...
    int holes[15][5][2];
    };
    ....
    int GameBoard::holes[15][5][2] = {{{0,0},{1,3},{2,5},{0,0},{0,0}},


    which compiles.
     
    , Jul 21, 2006
    #2
    1. Advertising

  3. free2klim

    free2klim Guest

    wrote:
    > g++ often has more insightful error messages:
    >
    > ref.cc:27: error: `int GameBoard::holes' is not a static member of
    > `class GameBoard'
    >
    > Adding static to the declaration within the class (.h), g++ reports:
    >
    > ref.cc:27: error: conflicting declaration 'int GameBoard::holes'
    > ref.cc:20: error: 'GameBoard::holes' has a previous declaration as `int
    > GameBoard::holes[15][5][2]'
    >
    > So, the types mismatch, we'll have to add the [][][] to the declaration
    > too.



    Thank you very much! That worked! I ended up with this, just like you
    said:

    class GameBoard{
    ...
    static int holes[15][5][2];
    };
    ....
    int GameBoard::holes[15][5][2] = {{{0,0},{1,3},{2,5},{0,0},{0,0}},
     
    free2klim, Jul 21, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scoots
    Replies:
    10
    Views:
    650
    Scoots
    Jun 19, 2007
  2. Travis
    Replies:
    1
    Views:
    317
  3. Jorgen Grahn
    Replies:
    2
    Views:
    325
    Jorgen Grahn
    Sep 8, 2008
  4. mlt
    Replies:
    2
    Views:
    907
    Jean-Marc Bourguet
    Jan 31, 2009
  5. sonic
    Replies:
    5
    Views:
    103
Loading...

Share This Page