Compilation errors in a subclass... very confused.

I'm programming a simulation of a harbour: there is a superclass, "Ship", which has two subclasses, "CruiseLiner" and "ContainerShip". "ContainerShip" then has two subclasses of its own, "CargoShip" and "MilitaryShip" (both of which can carry containers, hence the relationship).

I am having a lot of problems with compiling CargoShip. The header class is as follows:

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

#ifndef CARGOSHIP_H
#define	CARGOSHIP_H

class CargoShip : public ContainerShip
{
    private:
        int maxContainers;
    public:
        CargoShip(int ticket, HarbourMaster * hMaster);
        bool loadContainer();
        virtual ~CargoShip();
};

#endif	/* CARGOSHIP_H */



And the .cpp file:

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
/* 
 * File:   CargoShip.cpp
 * Author: benji
 * 
 * Created on September 1, 2010, 3:21 PM
 */

#include <cstdlib>
#include "CargoShip.h"
#include "ContainerShip.h"

using namespace std;

CargoShip::CargoShip(int ticket, HarbourMaster * hMaster) : ContainerShip(ticket, hMaster)
{
    hMaster->addToUnloadingQueue(this, false);
}

bool CargoShip::loadContainer()
{
    bool containerLoaded = false;
    if(numberContainers < maxContainers)
    {
        numberContainers++;
        containerLoaded = true;
    }
    return containerLoaded;
}

CargoShip::~CargoShip()
{
}



When I try to compile this I get the following errors:
In file included from CargoShip.cpp:9:
CargoShip.h:12: error: expected class-name before ‘{’ token
CargoShip.h:16: error: ‘HarbourMaster’ has not been declared
CargoShip.cpp:14: error: prototype for ‘CargoShip::CargoShip(int, HarbourMaster*)’ does not match any in class ‘CargoShip’
CargoShip.h:12: error: candidates are: CargoShip::CargoShip(const CargoShip&)
CargoShip.h:16: error: CargoShip::CargoShip(int, int*)
CargoShip.cpp: In member function ‘bool CargoShip::loadContainer()’:
CargoShip.cpp:22: error: ‘numberContainers’ was not declared in this scope
make[2]: *** [build/Release/GNU-Linux-x86/CargoShip.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


The ones that confuse me the most are the one from line 12 of CargoShip.h (expected class-name before ‘{’ token - even though the line before has 'class CargoShip'), and the one stating:

prototype for ‘CargoShip::CargoShip(int, HarbourMaster*)’ does not match any in class ‘CargoShip’

It is almost as if the class is not recognising itself.

Then there is the error on line 22 of the cpp file: 'error: ‘numberContainers’ was not declared in this scope'


numberContainers is a protected variable in the ContainerShip class. Why can't CargoShip then access it?

Finally this error:

In file included from CargoShip.cpp:9:

Which pertains to the #include "CargoShip.h" statement. Basically I have no idea what this even means.

If anybody could shed any light on these errors, I would appreciated it greatly. I have a feeling they might all be derived from some single, simple problem, but otherwise I'm stumped.
You haven't told the .h file what Harbourmaster is, you don't need the full definition of HarbourMaster in the .h file you can forward declare it class HarbourMaster; at the top of the .h and #include it in the .cpp file.

Remember that just because it is declared in one of the base classes of CargoShip this doesn't mean that the .h file will know about it because it does not get the full definition of the base classes these are defined in the .cpp file.

Other than that, I'm not sure, you have a few errors that I don't understand but fix that one then reply with what the situation is, fixing that one thing might fix some of the other too, compilers are strange beasts.
Topic archived. No new replies allowed.