Advertisement Jump to content
Sign in to follow this  

c++ passing static const to constructor

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have written a simple chess game in Java and have been trying to port it to C++ and Direct3d as a learning exercise. However I have run into a problem while trying to initialise the chesspiece objects the chesspiece constructor is as follows:
	ChessPiece(int x, int y, int type);
I also have a list of static const's repersenting all the chesspieces in the chesspiece class
	static const int WHITEKING	=	1;
	static const int WHITEQUEEN	=	2;
	static const int WHITEROOK1	=	3;
	static const int WHITEROOK2	=	4;
	static const int WHITEBISHOP1	=	5;
	static const int WHITEBISHOP2	=	6;
Then in my chessBoard class I try to initialise a piece like the following
ChessPiece* cp = new ChessPiece(4, 0, ChessPiece.WHITEKING);
that works in java but not in c++. I get the following error error C2226: syntax error : unexpected type 'ChessPiece' There is a problem with it accepting the static variable. The following will work
ChessPiece* cp = new ChessPiece(4, 0, 1);
Any ideas on how to get this to work. Thanks

Share this post

Link to post
Share on other sites
Well with your static consts you are making constants with a value, which is find but you can't do,


because item.1 is invalid. You cant have variables/constants with names of just a number and there for 'item' could never have that member.

If you simple want to access the statics that are within a class do:



Share this post

Link to post
Share on other sites
Original post by clearz



Also are you sure dynamically allocating an instance of ChessPiece necessary or is this another habbit of Java [grin].

if not then this would have been fine:

ChessPiece cp(4, 0, ChessPiece::WHITEKING);

Share this post

Link to post
Share on other sites
it looks like you should #define your peices tbh. like so:

#define WHITEKING 1
#define WHITEQUEEN 2
ChessPiece(int x, int y, WHITEKING);

or you could do:

static const int WHITEKING = 1;
ChessPiece(int x, int y, WHITEKING);

i think the ChessPeice<dot> has something todo with class namespace in java? correct me if im wrong


just realised this was probly in a class :/
read the posts above mine :p

Share this post

Link to post
Share on other sites
you might want to use an enumeration instead of consts.

namespace ChessPieceType
enum ChessPieceType

so it would be:
class ChessPiece
ChessPiece(int x, int y, ChessPieceType::ChessPieceType type)

and you create the variable like this:
ChessPiece cp(0, 0, ChessPieceType::WhiteKing);

Share this post

Link to post
Share on other sites
Thanks for the quick responces. The ChessPiece <dot> notation is used in java to access static members of a class but obviously the <::> notation in c++ does the same. It works fine now. I did think of using #define to define them but I never tought of using an enum. What is the benifits of this and can an enumeration be used in a switch statement.

dynamically allocating an instance is deffently a java habit of mine where all classes are dynamically allocated but there is another reason. I am using an array of chess pieces like the following.

ChessPiece* pieces[32];

pieces[0] = new ChessPiece(4,0, ChessPiece::WHITEKING);
pieces[1] = new ChessPiece(3,0, ChessPiece::WHITEQUEEN);
pieces[2] = new ChessPiece(0,0, ChessPiece::WHITEROOK1);
pieces[3] = new ChessPiece(7,0, ChessPiece::WHITEROOK2);
pieces[4] = new ChessPiece(2,0, ChessPiece::WHITEBISHOP1);
pieces[5] = new ChessPiece(5,0, ChessPiece::WHITEBISHOP2);
pieces[6] = new ChessPiece(1,0, ChessPiece::WHITEKNIGHT1);
pieces[7] = new ChessPiece(6,0, ChessPiece::WHITEKNIGHT2);
pieces[8] = new ChessPiece(0,1, ChessPiece::WHITEPAWN1);
pieces[9] = new ChessPiece(1,1, ChessPiece::WHITEPAWN2);
pieces[10] = new ChessPiece(2,1, ChessPiece::WHITEPAWN3);
pieces[11] = new ChessPiece(3,1, ChessPiece::WHITEPAWN4);
pieces[12] = new ChessPiece(4,1, ChessPiece::WHITEPAWN5);
pieces[13] = new ChessPiece(5,1, ChessPiece::WHITEPAWN6);
pieces[14] = new ChessPiece(6,1, ChessPiece::WHITEPAWN7);
pieces[15] = new ChessPiece(7,1, ChessPiece::WHITEPAWN8);
pieces[16] = new ChessPiece(4,7, ChessPiece::BLACKKING);
pieces[17] = new ChessPiece(3,7, ChessPiece::BLACKQUEEN);
pieces[18] = new ChessPiece(0,7, ChessPiece::BLACKROOK1);
pieces[19] = new ChessPiece(7,7, ChessPiece::BLACKROOK2);
pieces[20] = new ChessPiece(2,7, ChessPiece::BLACKBISHOP1);
pieces[21] = new ChessPiece(5,7, ChessPiece::BLACKBISHOP2);
pieces[22] = new ChessPiece(1,7, ChessPiece::BLACKKNIGHT1);
pieces[23] = new ChessPiece(6,7, ChessPiece::BLACKKNIGHT2);
pieces[24] = new ChessPiece(0,6, ChessPiece::BLACKPAWN1);
pieces[25] = new ChessPiece(1,6, ChessPiece::BLACKPAWN2);
pieces[26] = new ChessPiece(2,6, ChessPiece::BLACKPAWN3);
pieces[27] = new ChessPiece(3,6, ChessPiece::BLACKPAWN4);
pieces[28] = new ChessPiece(4,6, ChessPiece::BLACKPAWN5);
pieces[29] = new ChessPiece(5,6, ChessPiece::BLACKPAWN6);
pieces[30] = new ChessPiece(6,6, ChessPiece::BLACKPAWN7);
pieces[31] = new ChessPiece(7,6, ChessPiece::BLACKPAWN8);

If there is a better way to do this please let me know.

Share this post

Link to post
Share on other sites
Why do you need to differentiate duplicate pieces ?

One pawn, rook, bishop, and knight constant would be enough.

Actually, I'd probably subclass each piece from ChessPiece. This way, you can do :

pieces[x] = new Rook(0,0); and so on.

which would be cleaner, IMHO.

Share this post

Link to post
Share on other sites
"Why do you need to differentiate duplicate pieces ?"

The Game is accually a variation of chess where every piece has a color which is just as important to the game as the normal rules. This means that every piece is individual and needs to be labeled as so.

You are very right about subclassing also and using polymorphism for things like moving the pieces. I will probably implement it this way.

Share this post

Link to post
Share on other sites
It took that long for someone to suggest polymorphism :(

Also, you should look into some kind of data-driven design for initializing the pieces. It will be surprisingly handy when you start wanting to load/save games :)

Also, passing in the array positions to the constructor is ugly. Assuming you need the pieces to know where they are on the board at setup, and that you're setting up the whole board at once, better to pass in an array to a creation method:

class ChessPiece {
// for the benefit of the file format
static ChessPiece** createBoard(const int* types) {
// TODO: clean up magic numbers
ChessPiece** board = new ChessPiece*[64];
for (int i = 0; i < 64; ++i) {
int x = i % 8; int y = i % 8;
switch(type) {
case WHITEKING: board = new WhiteKing(x, y); break;
// etc. for other types
default: board = 0; break; // "empty square" type
// Probably this isn't best - depending on the rest of your design too

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!