Apothem
Apothem

Reputation: 7

Why doesn't my vector recognize my derived classes?

I have a huge problem, much bigger than what I'm going to post, but if someone can help me with this, then it will be smooth sailing from here.

Okay, so my vector of pointers REFUSES to recognize its derived classes. Let me explain.

I have a class of Player objects from which Warrior and Wizard derive from. The assignment asks me to store said objects (Warrior and Wizard) into a vector of pointers of Player objects. This is what I have so far:

vector<Player *> players; 
    cout << "Please enter the number of players" << endl;
    cin >> numOfPlayers;
    total = amountPlaying(numOfPlayers);
for(int i = 0; i < total; i++)
    {
        temp = members();
        if (temp == "Warrior" || temp == "warrior")
            players[i] = new Warrior();
        if (temp == "Wizard" || temp == "wizard")
            players[i] = new Wizard();
        else
        {
            cout << " wrong input, try again " <<endl;
            i--;
        }
        cin >> *players[i];
    }

Members function:

string members()
{
    string response;
    cout << "Please select a Warrior or Wizard" << endl;
    cin >> response;
    return response;
}

I have overloaded Warrior and Wizard to accept input via cin, but not Player (as the assignment said not to). Here is what the Warrior one looks like (Wizard is the same but with Wizard):

istream& operator>>(istream& in, Warrior& warrior)
{
    int base_strength, base_weapon_level, base_weapon_type;

    cout << "Please enter his/her weapon of choice (1 = sword, 2 = axe, 3 = bow, 4 = knife)" << endl;
    in >> base_weapon_type;
    cout << "Please enter his/her weapon level" << endl;
    in >> base_weapon_level;
    cout << "Please enter strength" << endl;
    in >> base_strength;

    warrior.set_power(base_strength);
    warrior.set_weapon_level(base_weapon_level);
    warrior.set_weapon_type(base_weapon_type);
    return in;
}

Now the problem is, I get this error (on the line with cin >>*players[i]):

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Player ' (or there is no acceptable conversion)

Now how do I fix this? I feel like it wont take it as a Warrior or Wizard, it ALWAYS takes it as a Player but I don't want that!

P.S. I know this all seems redundant and FAR from optimization but this is exactly how my professor wanted it done. :|

Any help would be appreciated as I've been stuck for HOURS on this! Thank thank thank you!

-Jon

Upvotes: 0

Views: 254

Answers (4)

beduin
beduin

Reputation: 8253

You are receieving this error because when you are dereferencing pointer to Player here, you get Player object, not Warrior or Wizard:

cin >> *players[i]

And Player object don't have operator>>() as you have said.

Possible solutions:
1. use dynamic_cast to cast from Player* to Wizard* or Warrior* at runtime. You can try it but there are more convenient ways. Example code:

Wizard *  pwizard = dynamic_cast <Wizard*>(players[i]);
if (pwizard) {
  // work with pwizard
} 
else {
  Warrior * pwarrior = dynamic_cast <Warrior*>(players[i]);
  if (pwarrior) {
    // work with pwarrior
  }
}

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

Your professor may have proscribed a base class operator>> because it wouldn't work. You can't make the canonical stream read operation virtual, because the first parameter is the stream, not the base class.

The usual solution is something like this:

class Player {
    ...
    virtual std::istream& read(std::istream& is) = 0;
    ...
};

std::istream& operator>>(std::istream& is, Player& p) { return p.read(is); }

Upvotes: 0

Ben Stott
Ben Stott

Reputation: 2218

The problem is that your operator>> is actually expecting to operate on a Warrior, yet the vector holds Players.

You can work from the derived class up the derivation tree, but working down isn't such a good idea. What you can do is implement operator>> for a generic Player class, and then operate on them as if they're either of Wizard or Warrior. Alternatively, inside your operator>>, you can call a pure polymorphic function that is implemented in both the derived classes, and this will then do the work for you.

Hope this helps

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361462

cin >> *players[i];

This is causing problem. Have you overloaded operator>> as:

std::istream & operator>>(std::istream & in, Player &player);

Upvotes: 0

Related Questions