mercredi 6 octobre 2021

Adding derived class object to vector

So in my code I'm trying to add unique_ptr to objects from derived class to vector of base class. I get this error:

E0304 no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::unique_ptr<Organism, std::default_delete<Organism>>, _Alloc=std::allocator<std::unique_ptr<Organism, std::default_delete<Organism>>>]" matches the argument list great_game C:\Users\48572\source\repos\great_game\great_game\World.cpp 14

The code of base class ( if you need more let me know, trying to put as little code as possible):

vector<unique_ptr<Organism>>  World::generate_organisms(int act_level)
{
    vector<unique_ptr<Organism>> organism_list = get_vector();
    coordinates sheep_pos(10, 2);
    //getting error in next line
    organism_list.push_back(make_unique<Sheep>(sheep_pos, *this));

    return organism_list;
}

Code of the derived class: .h file)

class Sheep : Organism
{
    Sheep( coordinates organism_pos, World* world);
};

.cpp file)

Sheep::Sheep( coordinates organism_pos, World* act_world)
    :
    Organism(organism_pos, act_world)
{
    this->armor = 0;
    this->damage = 2;
    this->health = 10;
    this->vigor = 10;
}

Aucun commentaire:

Enregistrer un commentaire