trouble with accessing member functions of vector components

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <vector>

using namespace std;

class tChessPiece
{
public:
    int x;
    int y;
    char shortName;
    bool whoseTurn;
        virtual bool 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;
}

What have I misunderstood?
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.
Last edited on
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <vector>

using namespace std;

class piece
{
public:
    int x;
    int y;
    char shortName;
    bool whoseTurn;
    void moveMe(int x2, int y2)
    {
        x=x2;
        y=y2;
    }
};

class square
{
public:
    piece *pieceOnSquare;
};

piece piece1;
square board[1][1];
vector <piece*> listingArray;

int main()
{
    piece1.moveMe(0,7);
    listingArray.push_back(piece1);
    board[listingArray[i].x][listingArray[i].y].pieceOnSquare=listingArray[i];
return 0;
}


Can i have any hints on what else is wrong?..
Now that your 'listingArray' vector consists of pointers don't you think you should be storing pointers in it and not a full copy of the object?
 
listingArray.push_back(&piece1);
Well I feel dumb. Thank you, it works now.
Topic archived. No new replies allowed.