In C++ I have an array of pointers to Player objects and want to fill it with Fickle objects where Fickle is a class that is derived from Player. This is because I want a general Player array that I can fill with different objects from different classes that all are derived from the Player class.
How can I do this?
I create an array of pointers to Player objects
Player ** playerArray;
Then initialize the array to a certain size
playerArray = new Player *[numPlayersIn];
But then the following does not work for some reason:
playerArray[i] = new Fickle(0);
How can I fill the playerArray with Fickle objects (Fickel is a class derived from Player) ?
Thanks.
UPDATE:
I get the error message (in Eclipse IDE):
expected ';' before 'Fickle'
I think it might be something to do with the definition of Fickle.
The Fickle.hpp file contains:
#pragma once
#include "player.hpp";
class Fickle: public Player {
public:
// constructor
Fickle(int initChoice){
choice = initChoice;
}
}
Is this OK or is there a problem with this?
The Player class header file has:
class Player {
private:
public:
int getChoice();
int choice; // whether 0 or 1
virtual void receive(int otherChoice); // virtual means it can be overridden in subclases
};
The receive method will be overridden in Fickle and other classes derived from the Player class
UPDATE 2:
OK I think the error is actually due to a different part of the code.
Player defines a method receive:
virtual void receive(int otherChoice);
That should be overridden by the subclass Fickle but the definition in Fickle:
void Fickle::receive(int otherChoice) {}
gives the error:
no 'void Fickle::receive(int)' member function declared in class 'Fickle'
But I don't know why this is because receive is defined in the Player class?