Program is "calling a pure virtual method"...?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 
 * File:   Ship.h
 * Author: benji
 *
 * Created on September 1, 2010, 3:08 PM
 */

#ifndef SHIP_H
#define	SHIP_H

#include "Bay.h"

class HarbourMaster;

class Ship
{
    protected:
        int ticketNumber;
        HarbourMaster * hm;
        Bay * atBay;
    public:
        Ship(int ticket, HarbourMaster * hMaster);
        int getTicketNumber();
        virtual void inviteToDock(Bay * bay1, Bay * bay2);
        void setBay(Bay * bay);
        virtual void undock() = 0;
        virtual ~Ship();
};

#endif	/* SHIP_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 
 * File:   CruiseLiner.h
 * Author: benji
 *
 * Created on September 1, 2010, 3:23 PM
 */

#include "Ship.h"

#ifndef CRUISELINER_H
#define	CRUISELINER_H

class CruiseLiner : public Ship
{
    private:
        
    public:
        CruiseLiner(int ticket, HarbourMaster * hMaster);
        void inviteToDock(Bay * bay1, Bay * bay2);
        void undock();
        virtual ~CruiseLiner();
};

#endif	/* CRUISELINER_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define toMilliseconds(x) (x * 100) //Conversion of minutes to milliseconds;
                                    //100 milliseconds simulates one minute
#define DOCKED_PERIOD 60 //60-minute waiting period between docking and undocking

#include <cstdlib>
#include <iostream>
#include "CruiseLiner.h"
#include "HarbourMaster.h"

using namespace std;

CruiseLiner::CruiseLiner(int ticket, HarbourMaster * hMaster) : Ship(ticket, hMaster)
{
    cout << "CRUISE LINER CREATING..." << endl;
    hMaster->addToLoadingQueue(this);
    cout << "CREATED" << endl;
}

void CruiseLiner::inviteToDock(Bay * bay1, Bay * bay2)
{
    Bay * bay;
    //choose a bay at random to work with
    switch(rand() % 2)
    {
        case 0: bay = bay1; break;
        case 1: bay = bay2; break;
    }
    boost::mutex::scoped_lock bayLock(*(bay->getMutex()));
    while(!bay1->isFree() && !bay2->isFree())
    {
        bay->getCond()->wait(bayLock);
    }
    hm->dockAtBay(bay, false);
}
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.
Hi there,

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.

Kind Regards,

Phil.
Topic archived. No new replies allowed.