'Using polymorphism with vectors in C++. I'm getting errors when attempting to add a pointer to a derived class to a vector of type pointer to Base

I'm new to C++ and could really appreciate some help. I have a Base class and several class types derived from this. I'm trying to use polymorphism to add these derived objects to a vector of type Base class. After spending some time looking into this, it seems as though the recommended way to do this is using pointers. However, when I try and implement this I get an error message (in intellisense) stating that there is no known conversion between the base and derived class types.

I've included some example code for what I'm trying to achieve. Apologies for such a silly / trivial example, but I'm not allowed to show any of the actual code I'm working on. In this example, in the header file I have an abstract base class "Meal", and two types of derived classes : "SingleCourseMeal" and "ThreeCourseMeal". In the "Restaurant" class I have a vector of type unique_ptr<Meal>

In the cpp file I read from file and create a unique_ptr of either a SingleCourseMeal or a ThreeCourseMeal type, depending on the information in the file. I assign the relevant variables to the dereferenced pointer (from information in the file), and finally add the unique_ptr to the unique_ptr vector in the Restaurant object. However, I'm unable to add a pointer to a ThreeCourseMeal or a SingleCourseMeal to the unique_ptr<Meal> vector.

Could anyone advise where I'm going wrong, and/or how best to implement polymorphism like this? (i.e. populating a vector of type Base with Derived objects)

header file:

class Meal
{
public:

    String cuisineType;
    double prepTime;

    virtual void setRecipe() = 0;

};


class MultipleCourseMeal : public Meal
{
public:

    Vector<String> courses;
    double intervalBetweenCourses;

    void setRecipe() override {}
};


class SingleCourseMeal : public Meal
{
    void setRecipe() override {}
};


class Restaurant
{
    Vector<std::unique_ptr<Meal>> meals;
};

cpp file:

String cuisineType = getCusineTypeFromFile();
double prepTime = getPrepTimeFromFile();

if (numberOfCourses > 1) {

    std::unique_ptr<MultipleCourseMeal> meal = std::make_unique<MultipleCourseMeal>();
    
    Vector<String> courses = getCoursesFromfile();
    double intervalBetweenCourses = getIntervalFromFile();

    meal->cuisineType = cuisineType;
    meal->prepTime = prepTime;
    meal->courses = courses;
    meal->intervalBetweenCourses = intervalBetweenCourses;

    Restaurant.meals.push_back(meal);

}
else {
    std::unique_ptr<SingleCourseMeal> meal = std::make_unique<SingleCourseMeal>();
    meal.cuisineType = cuisineType;
    meal.prepTime = prepTime;

    Restaurant.meals.push_back(meal); //THIS IS WHERE THINGS GO WRONG
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source