-
October 21st, 2010, 09:48 PM
#1
Array of pointers issue
I'm attempting to make a game of 3D chess, in which there are piece, board, and coordinates classes. In the board class, there is an [8][8][8] array of pointers to pieces. In the piece's constructor, I have the relevant pointer in the board point to the piece, but it doesn't seem to work correctly. When I try to print the name, it gives me a strange character, which when cast to int is -52, instead of 'Q'. Here is the relevant code:
Main.cpp:
board* homeBoard = 0;
int main() {
int i;
setup();
piece q ('Q', 1, coordinates(5,4,2));
piece* pq = homeBoard->lookup(5,4,2);
cout << pq->name;
cin >> i;
}
void setup() {
delete homeBoard;
homeBoard = new board();
piece::setHomeBoard(homeBoard);
}
Piece.cpp:
void piece::setHomeBoard(board* pb) {
homeBoard = pb;
}
piece:iece(char id, bool team, coordinates c) {
this->name = id;
//otherdata = 0; //Stored: **** T M C M(m)
//otherdata |= (team&1)<<3;
//otherdata |= findMultiMove()&1;
//loadMoveTypes();
//location = c;
//whereMove = whereCapt = whereCast = 0;
//findMoves(true);
homeBoard->place(*this,c);
}
Board.cpp:
piece* board::lookup(coordinates c) const {
return lookup(c.getX(), c.getY(), c.getZ());
}
piece* board::lookup(int x, int y, int z) const {
if(0<=x && x<=7 && 0<=y && y<=7 && 0<=z && z<=7) return table[x][y][z];
return 0;
}
void board:lace(piece p, int x, int y, int z) {
table[x][y][z] = &p;
}
Does anybody know what went wrong? Also, the coordinates class is working perfectly, no matter what I throw at it, so I don't think it's that. And the header files work too.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|