I am developing a ship simulation, with a base class "Ship". Ship has two direct subclasses, "ContainerShip" and "CruiseLiner". ContainerShip has two further subclasses, "CargoShip" and "MilitaryShip".
Ship has a pure virtual method called "inviteToDock". ContainerShip keeps this pure virtual, while CruiseLiner, CargoShip and MilitaryShip implement it. The trouble is, when I call the method on a CruiseLiner object, the program terminates, telling me that I have called a pure virtual method. When I call it on either CargoShip or MilitaryShip objects, it works correctly.
Code for Ship.h, CruiseLiner.h and CruiseLiner.cpp is below. Can anybody figure this out?
Update: Forgot that I removed the "= 0" from the end of the inviteToDock declaration in CruiseLiner.h . It was there when the method wasn't working; I removed it for testing purposes.
A pure virtual method call is almost always caused by attempting to call a virtual method from the constructor or
destructor of the object (or, more precisely, at construction time or destruction time of the object). You haven't
posted enough code to see if that is what is going on.
Based on what you have posted it looks like you haven't implemented the method "undock()" - you have only said that you have in your class definition. Therefore when you call this method it is trying to use the base classes implementation but this is a pure virtual hence the error.