Advertisement

Problem with structures

Started by October 25, 2002 12:04 PM
1 comment, last by CompuWizJacob 22 years ago
Can anyone tell me why I can''t declare a constant int inside a class? Here''s my code: struct ship { ship() {}; char directon; int movesLeft; const int maxMoves = 4; int xPosition; int yPosition; void move(int direction, int *xAxisMove, int *yAxisMove, int *xPosition, int *yPosition); void changeManInMast(int *movesLeft, bool *manInMast, int *visibility); }; I get this error message: 8 cShip.h making `maxMoves'' static

      class ship{public:ship() {};char directon;int movesLeft;static const int maxMoves;int xPosition;int yPosition;void move(int direction, int *xAxisMove, int *yAxisMove,int *xPosition, int *yPosition);void changeManInMast(int *movesLeft, bool *manInMast, int *visibility);};const int ship::maxMoves = 4;  


or


  class ship{ public:ship():maxMoves(4) {};char directon;int movesLeft;const int maxMoves;int xPosition;int yPosition;void move(int direction, int *xAxisMove, int *yAxisMove,int *xPosition, int *yPosition);void changeManInMast(int *movesLeft, bool *manInMast, int *visibility);};  


[edited by - smart_idiot on October 25, 2002 2:57:52 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
One important point: a struct merely DESCRIBES data, i.e. how it should be allocated, it does not create one.

You create one when you make a variable of that type. Instead of the symbolic const, you could use an enumeration, or one of the options smart_idiot mentioned. This is also more efficient because with your ship struct, you make a seperate maxMoves for every variable of type ship you create. With the other ways mentioned, there is just one copy for all instances.





*Scribble with excessive amounts of loops and curls*
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement