-1

I'm trying to write a program with four classes: vehicle, car, airplane and flying car.

Flying car inherits from car and airplane, car and airplane both inherit from vehicle.

vehicle.h

class vehicle
{
public:
    vehicle();
    vehicle(char*, int, char*);
    virtual void setmodel(char*);
    //more functions..
    ~vehicle();
protected:
    char* model;
    int speed;
    char* color;
};

car.h

#include "vehicle.h"
class car:public virtual vehicle
{
public:
    car();
    car(char*, int, char*);
    void driving(std::ostream&);
};

airplane.h

#include "vehicle.h"
class airplane:public virtual vehicle
{
public:
    airplane();
    airplane(char*, int, char*);
    void flying(std::ostream&);
};

flyingCar.h

#include "car.h"
#include "airplane.h"
class flyingCar: public car, public airplane
{
public:
    flyingCar();
    flyingCar(char*, int, char*);
};

Also, the source files for every class include the related header files (so thart vehicle.cpp includes vehicle.h, car.cpp includes car.h and so on.. ).

The main function includes the vehicles.h header and ifndef...

When I compile I get an error for redefinition of class vehicle in car.h, but since I already used the virtual I don't understand how I could solve the problem.

I suspect including the class headers in the class source files may be incorrect, since the inherited headers are already included in the inheriting headers themselves, but if I don't include them here the compiler will not refer to the class prototypes. As an attempt I enclosed everything in ifndef.. with no positive result.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
maja
  • 697
  • 5
  • 18
  • 1
    @CoryKramer Then the code would be subject to the diamond inheritance problem. See http://www.cprogramming.com/tutorial/virtual_inheritance.html – Daniel Jun 17 '15 at 16:41
  • @Daniel You are correct! – Cory Kramer Jun 17 '15 at 16:44
  • @πάντα ῥεῖ that's much more precise, thanks. (still it is difficult to find that question if you're looking through the standard search), – maja Jun 17 '15 at 16:59
  • @maja Well, it's easier to find, if you know [the actual problem are the include guards](http://stackoverflow.com/search?tab=votes&q=%5bc%2b%2b%5dredefinition%20error%20include%20guard) ;-). – πάντα ῥεῖ Jun 17 '15 at 17:01

1 Answers1

4

You should use include guards

#ifndef VEHICLE_H
#define VEHICLE_H

class vehicle
{
public:
    vehicle();
    vehicle(char*, int, char*);
    virtual void setmodel(char*);
    //more functions..
    ~vehicle();
protected:
    char* model;
    int speed;
    char* color;
};

#endif

Because both car.h and airplane.h use #include "vehicle.h" so the Vehicle class will be defined twice when both of their respective headers are included in flyingCar.h.

In general you should have header guards around all your headers, or equivalent such as #pragma once, etc.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • All right, so that's where they go (I had tried to place them in the source files..). I'm used to place them in the main source file, but I guess that's not a given (nor the most efficient way). Thanks. – maja Jun 17 '15 at 16:51