Hey guys, i have this problem in my programe: I want to create a vector something like vector <animal> a; where the class animal is abstract. So i don't know if i can declare a vector of this abstract class and then fill it with the derivatives classes, and then use them in my code. Something like:
@Thomas1965 thanks you for the answer
Do you know how could i put derivatives classes on that vector that you have declared, or should i assign the pointers from the vector to the derivatives classes?
int main()
{
vector <animal *> animals; // empty vector
horse *newHorse = new horse;
animals.push_back(newHorse);
// use
for(size_t i = 0; i < animals.size(); ++i)
{
delete animals[i];
}
animals.clear(); // a bt redundant as exiting, but...
}
And you don't need new (or delete) if you're working with a limited set of animals;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
vector <animal *> animals; // empty vector
Horse horse[2];
Cat cat[2];
Dog dog[2];
// etc
// all animals must exist for longer than the animals vector
// so new/delete approach is safer in general
animals.push_back(&horse[0]);
animals.push_back(&horse[1]);
animals.push_back(&cat[0]);
animals.push_back(&cat[1]);
animals.push_back(&dog[0]);
animals.push_back(&dog[1]);
// etc
}
Andy
PS using a smart pointer would be better than a raw pointer
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
class Piece
{
public:
Piece () : letter ('o') {}
~Piece(){}
void setletter(char _letter) {letter =_letter;}
char getletter() {return letter;}
virtualvoid move()=0;
private:
char letter;
};
int main(){
Piece *table [8][8];
char _letter = 'o';
int i=0, j=0;
table[i][j]->setletter(_letter); //here is a warning that says: 'table [i][j]' is not used uninitualized in this function
cout << table[i][j]->getletter(); //i can print this
cout <<endl<< "table full of 'o': "<< endl<<endl;
for (i=0; i<8; i++){ cout <<endl;
for (j=0; j<8; j++){
table[i][j]->setletter(_letter);
cout << table[i][j]->getletter(); //but not this
}
}
}
I can print just 1 by 1 the elements of my array but not everyone in just once.
Line 25: You've created an 8x8 array of pointers, not Pieces. Those pointers are uninitialized.
Line 28,29,33,34: You're trying to use the pointer at table[i][j] which is garbage.
Get rid of the * on line 25 and change your references from -> to .
Edit: Based on your earlier post with derived classes, Rook and Knight, I'm guessing you do want pointers (for polymoprphism). You will need to initialize the array of pointers in some manner (perhaps setting empty cells to nullptr). In any case, you will need to be careful to only reference those cells that have valid pointers to Pieces.
@AbstractionAnon thank you. You said it in your edit, i can't use Pieces, i have to use pointers because Pieces is abstract. But i don't know why i can print line 28 and 29 but not the 'for (31, 32, 33 and 34), it just finishes my program, maybe because I'm trying to print garbage which is not what i want.
Why did you change to using an array from the vector? You had almost everything correct, except the fact that you declared a vector with 6 uninitialized elements and tried to access some of these uninitialized elements causing the program to crash.
Because i want to fill the table, which is bidimensional array of pointers, with the elements from the vector. Then print table[i][j]->getletter(); with 'o' where there is no Piece and the letter from the Piece when it's busy.
I have Piece *table [8][8];vector<Piece *> p; which i dont have any problem any more, and char _letter = 'o';.
If this is a chess board, shouldn't there just be 32 pieces?
The chess board has 64 squares, of course.
Andy
PS vector <Piece*> p (6);
does not create a vector of six uninititalized elements; the elements are value initialized (if no constructor is avaliable) or default constructed (it there is a default constructor.)