I am trying to create a program that takes an order for food and prints it. I have a base class Food
that has a pure virtual function in it. The Food class has 2 subclasses Pizza
and Dessert
. I am trying to create an array Food
in mine main
, so when the client orders Pizza
or Dessert
, it will be saved in the array Food
. But every time I try, I get an error message. How do I put two elements together if I want to use a loop to go through each element ordered by the client? This is my code:
int main()
{
Dessert d("brownie");
Pizza p("BBQ delux");
Food array[2] = {d,p};
}
This is my error message. (NOTE: get_set_price()
and print_food()
- my pure virtual functions, which are defined in the base class and implemented in two subclasses)
main.cpp:37:14: error: invalid abstract type βFoodβ for βarrayβ
Food array[2] = {d,p};
In file included from main.cpp:4:0:
Food.h:5:7: note: because the following virtual functions are pure within βFoodβ:
class Food
^
Food.h:20:15: note: virtual void Food::get_set_price()
virtual void get_set_price()=0;
^
Food.h:27:15: note: virtual void Food::print_food()
virtual void print_food()=0;
^
main.cpp:37:22: error: cannot allocate an object of abstract type βFoodβ
Food array[2] = {f,o};
^
source
share