0

Basically this:

Car and Truck are both derived from the Vehicle class.

Car.h

#include "Vehicle.h"
class Car : public Vehicle {
//blah blah blah

Truck.h

 #include "Vehicle.h"
 class Truck : public Vehicle {
 //blah blah blah

Main.cpp

#include "Car.h"
#include "Truck.h"

My issue is that I get a class redefinition error on Truck (due to it calling Vehicle a second time) when I have the #include line there, but when I remove it, I have the "expected class name before { token".

I get that with Main -> Car -> Vehicle Main -> Truck -> Vehicle (redefinition)

will cause an error.

But if I remove the #include "Vehicle.h" from Truck it also causes another error where it expects a class name.

user3551329
  • 3
  • 1
  • 1
  • 6

1 Answers1

6

As @ErikW pointed out you need to use include guards. See here and here.

Example

Foo.h

#ifndef FOO_H // This needs to be unique in each header
#define FOO_H

... code goes in here ...

#endif
Community
  • 1
  • 1
James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • I still get errors when using include guard though. With include guard it says "expected class name before { token" – user3551329 Apr 06 '15 at 03:38
  • @user3551329 Perhaps you could show more of your code (e.g., Vehicle.h), maybe you have a circular include issue. You'll get the best help if you provide an [mcve](http://stackoverflow.com/help/mcve). – James Adkison Apr 06 '15 at 03:41
  • my Vehicle header file has no includes in it. And my Vehicle.cpp file has only an #include Vehicle.h. I'll try to reproduce it. – user3551329 Apr 06 '15 at 03:52