Reputation: 51
I am learning Object Oriented programming. Here is a simple code snippet that I have written:
class Food {
protected:
int id;
FoodTypes type;
string name;
public:
Food(int id, FoodTypes type, string name) {
this->id=id;
this->type=type;
this->name=name;
}
virtual ~Food() = 0;
};
class Dish: public Food {
protected:
double cost;
public:
Dish(int id, FoodTypes type, string name, double cost) : Food(id, type, name) {
this->cost=cost;
}
void display() {
cout<<id<<" "<<type<<" "<<name<<" "<<cost<<"\n";
}
~Dish() {}
};
I get the error:
/tmp/ccW3e3PZ.o: In function 'Dish::~Dish()': main.cpp:(.text._ZN4DishD2Ev[_ZN4DishD5Ev]+0x25): undefined reference to 'Food::~Food()'
Based on the solution mentioned here, I need to define the destructor of my base class (~Food
), but IMO I cannot do that since I need it to be a pure virtual
function (as suggested here). If not for the destructor, there's no other function within the base class that can be delegated to be pure virtual
(and the base class would no longer be abstract/interface).
So how do I resolve the error? Thanks!
Upvotes: 0
Views: 244