declaring subclasses in C++
hello, i'm trying to create subclasses to a class, but i get the same error to all my subclasses: expected class-name before*'{' token. Ive checked on other threads and site since it seems that this problem is very common. here is my code:
MoyenDeTransport.h:
Code:
class MoyenDeTransport
{
double litre;
public:
MoyenDeTransport(double);
double getLitre();
//void parcourir(int distance);
};
and here is my subclass:
Camion
Code:
class Camion: public MoyenDeTransport
{
public:
Camion();
};
as a matter of fact, i get one warning and one error:
warning: In File included from camion.cpp:1,
error: expected class-name before*'{' token
here is my camion.cpp:
Code:
#include "camion.h"
Camion::Camion()
{
}
and just in case, here is my MoyenDeTransport.cpp:
Code:
#include "moyendetransport.h"
MoyenDeTransport::MoyenDeTransport(double ammount)
{
litre = ammount;
}
I dont think i'm doing anything flagrantly wrong, but i'm sure its one small thing that i'm missing...
What do you guys think?
Paat
Re: declaring subclasses in C++
I think you need to put a
Code:
#include "MoyenDeTransport.h"
in your moyen.h to let the derived class know here the base class is
Re: declaring subclasses in C++
i tried that and i get an infinite amount of errors, because by adding that line of code, you're refering moyen.cpp to moyen.h, which is linked to moyen.cpp, which includes moyen.h, which is linked to moyen.cpp, which.... its an infinite loop.
Re: declaring subclasses in C++
why would a header file include moyen.cpp?
Your camion class inherits MoyenDeTransport class, so Camion.h needs #include of MoyenDeTransport.h. Theres no need for MoyenDeTransport.h to include camion.h or any cpp files.
Also, you should really guard your header files from multiple inclusion. It makes things a lot easier later on when multiple objects depend on the same base classes.
Re: declaring subclasses in C++
If your include directives leads to mutliple inclusions to the same file,
you need to prevent from mutliple class definition with #ifndef ID with a unique id you define yourself
So insert in your MoyenDeTransport.h :
Code:
#ifndef MOYENDETRANSPORT_H
#define MOYENDETRANSPORT_H
//definition of clas MoyenDeTransport
#endif
and in your Camion.h :
Code:
#ifndef CAMION_H // or any unique id you define yourself
#define CAMION_H
//defintion of class Camion
#endif