Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ fill array with objects derived from array type
    primarykey
    data
    text
    <p>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. </p> <p>How can I do this?</p> <p>I create an array of pointers to Player objects</p> <pre><code>Player ** playerArray; </code></pre> <p>Then initialize the array to a certain size</p> <pre><code>playerArray = new Player *[numPlayersIn]; </code></pre> <p>But then the following does not work for some reason:</p> <pre><code>playerArray[i] = new Fickle(0); </code></pre> <p>How can I fill the playerArray with Fickle objects (Fickel is a class derived from Player) ?</p> <p>Thanks.</p> <p>UPDATE:</p> <p>I get the error message (in Eclipse IDE):</p> <pre><code>expected ';' before 'Fickle' </code></pre> <p>I think it might be something to do with the definition of Fickle.</p> <p>The Fickle.hpp file contains:</p> <pre><code>#pragma once #include "player.hpp"; class Fickle: public Player { public: // constructor Fickle(int initChoice){ choice = initChoice; } } </code></pre> <p>Is this OK or is there a problem with this?</p> <p>The Player class header file has:</p> <pre><code>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 }; </code></pre> <p>The receive method will be overridden in Fickle and other classes derived from the Player class</p> <p>UPDATE 2:</p> <p>OK I think the error is actually due to a different part of the code.</p> <p>Player defines a method receive:</p> <pre><code>virtual void receive(int otherChoice); </code></pre> <p>That should be overridden by the subclass Fickle but the definition in Fickle:</p> <pre><code>void Fickle::receive(int otherChoice) {} </code></pre> <p>gives the error:</p> <pre><code>no 'void Fickle::receive(int)' member function declared in class 'Fickle' </code></pre> <p>But I don't know why this is because receive is defined in the Player class?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload