Hi, I'm trying to make a chess game and I could have sworn I hadn't changed the code at all since a day or two ago, and it ran perfectly well then, but it doesn't now.
Essentially, I make a vector to hold pointers to chess pieces, but when I try to access a member of those chess pieces through the vector it doesn't work.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <vector>
usingnamespace std;
class tChessPiece
{
public:
int x;
int y;
char shortName;
bool whoseTurn;
virtualbool isMoveValid (int x1,int y1,int x2,int y2) = 0;
void moveMe(int x2, int y2)
{
x=x2;
y=y2;
}
};
class Rook: public tChessPiece
{
bool isMoveValid(int x1,int y1,int x2,int y2)
{
return ((x1==x2||y1==y2));
}
};
Rook Black_Rook1;
void substantiatePieces()
{
Black_Rook1.moveMe(0,7);
Black_Rook1.shortName='r';
Black_Rook1.whoseTurn=0;
}//end substantiatePieces
vector <tChessPiece> * listingArray;
void vectorListMake()
{
listingArray->push_back(Black_Rook1);
}
class square
{
public:
tChessPiece *piece;
bool occupied =0;
};
square board[8][8];
void placePieces()
{
//cout<<listingArray.size();
for (int i=0; i<32; i++)
{
board[listingArray[i]->x][listingArray[i]->y].piece=listingArray[i];
board[listingArray[i]->x][listingArray[i]->y].occupied=1;
}
}
//if I use listingarray[i]->x i get "error: base operand of '->' has non-pointer type 'std::vector<tChessPiece>' "
//when I thought I declared the vector as being 'pointer' type when i put "vector <tChessPiece> * listingArray;"
//but if i use listingArray[i].x, i get "error 'class std::vector<tChessPiece>' has no member named 'x'
//which says it thinks Im trying to access a member function of an instance of the vector class, rather than the tChessPiece class.
int main()
{
substantiatePieces();
vectorListMake();
placePieces();
return 0;
}
You have SO many more issues then that. The vector is your biggest problem (aside from scope management that is), it looks like you want to use polymorphism to handle all of the chess pieces in the same array right? Then you need a vector of pointers, not a pointer to a vector. No matter what you do though this is going to crash at Line 60 right now anyway.
well thats good to know i guess.. thank you
but I thought i had created a vector of pointers.
Should I have written vector <tChessPiece*> listingArray; ? that gives an error when I try to use push_back though.
and yes I forgot to edit line 60, thank you