1

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?

10
  • 1
    What error do you get? What is definition of Fickle (first line with the base classes)? Commented Oct 1, 2010 at 20:58
  • 1
    Any reason you're not using std::vector? std::vector<Player*> is simple enough. Even better (since you need to manage those pointers) would be boost::ptr_vector<Player> or std::vector<shared_ptr<Player>>. Commented Oct 1, 2010 at 20:58
  • 1
    What do you mean, "does not work"? As far as the pointer types are concerned it does work, so there must be some error elsewhere in your code. Does Fickle have the required constructor? Is 0 <= i < numPlayersIn? Commented Oct 1, 2010 at 21:00
  • 1
    Does Fickle derive from single Player base (i.e. no multiple inheritance)? Commented Oct 1, 2010 at 21:04
  • @ybungalobill: Irrelevant. This idea should work even if there is multiple inheritance. Commented Oct 1, 2010 at 21:16

4 Answers 4

5

While you probably should be using a vector instead, there's no real reason a dynamically allocated array can't work. Here's a bit of working demo code:

#include <iostream>

class Player {
public:
    virtual void show() { std::cout << "Player\n"; }
};

class Fickle : public Player {
public:
    virtual void show() { std::cout << "Fickle\n"; }
};

int main() {
    Player **p = new Player *[2];
    p[0] = new Player;
    p[1] = new Fickle;

    for (int i=0; i<2; i++)
        p[i]->show();

    for (int i=0; i<2; i++)
        delete p[i];
    delete [] p;
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why is Player declared as a struct here? Does it make a difference if Player is a class instead of a struct?
@tree-hacker: it just saves putting in a public:. That's the only real difference it makes. To save confusion, I've made both classes with public:.
2

It looks like you forgot a semicolon at the end of Fickle class:

class Fickle: public Player {
    // ...
}; // <---- this semicolon

Maybe somewhere else.

UPDATE 2: When you override a virtual function in the derived class you must declare it there too.

3 Comments

For such a common mistake it's surprisingly hard to track down.
@Mark Ransom: Blame C++ preprocessor. It's especially hard when it comes from the end of some include. When I was a newbie I wasted a hour to track this error once.
OK I didn't know you had to declare overridden functions in the derived class as well.
1

If it's not compiling correctly, then you probably didn't #include the header that defines Fickle in the same source file you have that code. Usually an error like that means the compiler doesn't know what Fickle is.

Comments

0

Does Fickle have a constructor that accepts an int or pointer type? Is it privately derived from Player? Otherwise it looks like this should work.

1 Comment

Yes Fickle does have an int constructor. It is publicly derived from Player I think (see code in question) but am not too sure about derived classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.