vector and array problems

Hello guys, i need some help with this.

Do you know how can i print a variable from an objetc which is inside the vector:

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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Piece
{
public:
    Piece (char _letter) : letter (_letter), colorP (true) {}
    ~Piece(){}
    void setletter(char _letter) {letter =_letter;}
    void setcolorP (bool _colorP) {colorP = _colorP;}

    char getletter() {return letter;}
    int getcolorP () {return colorP;}

    virtual void  posicion ()=0;
    virtual void  mover()=0;

private:
    char letter;
    bool colorP;
};

class rook : virtual public Piece
{
public:
    rook (char _letter) : Piece (_letter){}
    ~rook (){}
    void mover() { }
    void posicion() {}
};

class knight : virtual public Piece
{
public:
    knight (char _letter) : Piece (_letter){}
    ~knight(){}
    void mover() { }
    void posicion() {}
};

//... rest of the classes


int main(){

    knight c ('c');
    rook t ('t');
    
    vector <Piece*> p (6);
    p.push_back(&t);
    p.push_back(&c);
    cout <<"letter "<< p(2)->getletter(); //here is the compilation problem. i want to print the char a
}
A vector uses the same semantics as an array. So if you want to access the 3rd element of the vector, you use:

p[2]
Last edited on
Topic archived. No new replies allowed.