1

I have Road and Car classes that are in their own .cpp and .h files. I include the .h file for the Road class in the Car header. I am using the Road class as a parameter for functions in the Car classes. I have static variables in the car class I need access to in the road class.

The compiler is not recognizing the Road type in the Car Class, and I can't figure out why.

Road.h

#ifndef ROAD_H
#define ROAD_H

#include <iostream>
#include "car.h"

using namespace std;

class Road
{
public:
  // class functions
private:
  // member variables
};

#endif

Car.h

#ifndef CAR_H
#define CAR_H

#include <iostream>
#include "road.h"

using namespace std;

class Car
{
public:
  // class functions
  void enter_a_road(Road& r1, const short left_pos);
private:
  // member variables
};

#endif

Error Message:

In file included from Road.h:11:0,
                 from Road.cpp:6:
Car.h:68:23: error: 'Road' has not been declared
     void enter_a_road(Road& r1, const short left_pos);
                       ^
Bristles
  • 41
  • 1
  • 8
  • What is the error message? – 463035818_is_not_an_ai Dec 03 '15 at 16:54
  • 6
    [You have a circular dependency](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – NathanOliver Dec 03 '15 at 16:54
  • 1
    road includes car and car includes road includes car includes road includes car... – crashmstr Dec 03 '15 at 16:54
  • You are including car.h in road.h, and road.h in car.h. This is not possible. – Daniel Daranas Dec 03 '15 at 16:54
  • You most likely have a circular dependency. Car needs to know about road, and road needs to know about Car. (At least I assume you have a `Car` somewhere inside `class Road` – AndyG Dec 03 '15 at 16:54
  • What you should try to solve this conundrum is to use [forward declaration](http://stackoverflow.com/a/553869/96780). – Daniel Daranas Dec 03 '15 at 16:56
  • Try this: `void enter_a_road(class Road& r1, const short left_pos);` – zdf Dec 03 '15 at 16:59
  • In addition to forward declarations, you should use either header guards `#ifndef SOME_KEY_WORD ..... #endif` or `#pragma once`. Either (both are extraneous) will prevent circular dependency from biting you. – Andrew Falanga Dec 03 '15 at 17:07
  • I forgot to show that I had #ifndef ... #endif in the headers. Too second nature for me to think about them; I edited the code in the question to include the fact I have included this. – Bristles Dec 03 '15 at 17:17

1 Answers1

5

You have a circular dependency. The quickest way to solve it is to use a forward declaration.

#include <iostream>

using namespace std;

class Road;

class Car
{
public:
  // class functions
  void enter_a_road(Road& r1, const short left_pos);
private:
  // member variables
};

This is acceptable because the compiler doesn't need to know about the internal structure of Road in order to compile the function declaration.

The same could probably be done in the Road.h, though I don't know why the include is there to begin with.

Community
  • 1
  • 1
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
  • I have static variables about the Car class I need access to in the Road class is the reason I have car included in road. – Bristles Dec 03 '15 at 17:03
  • @Bristles I'm sure why this is the case. It is difficult to know from your posted code. Consider having the `Road` class access these properties via *getter* functions exposed in `Car`. – Andrew Falanga Dec 03 '15 at 17:10
  • @AndrewFalanga That would still require the inclusion of the Car.h file in Road.h – Bristles Dec 03 '15 at 17:14
  • @Bristles Only if you made calls to those *getters* from within the header file. This usually isn't the case. – Andrew Falanga Dec 03 '15 at 17:25