User-created header calling C2061: Syntax error: identifier 'classname'

So, I seem to expect this to be a simple answer, but I hack it a bit for a while and cannot solve this problem. Therefore, I have a specific Intersection class which, when included in any other header, gives me:

error C2061: syntax error : identifier 'Intersection'

This is my Intersection header:

 #ifndef INTERSECTION_H #define INTERSECTION_H #include "Coord.h" #include "Road.h" #include "TrafficLight.h" class Intersection { private: int id; Coord * midPoint; Road * northRoad; Road * eastRoad; Road * westRoad; Road * southRoad; TrafficLight * trafficLight; public: Intersection(int, Coord *, Road *, Road *, Road *, Road *); ~Intersection(); void transitionTrafficLight(); int getId(); Road * getNorthRoad(); Road * getEastRoad(); Road * getWestRoad(); Road * getSouthRoad(); TrafficLight * getTrafficLight(); }; #endif 

Now, if I try to use this class elsewhere, I get an error. For example:

 #ifndef ROAD_H #define ROAD_H #include "Coord.h" #include "Intersection.h" #include <string> class Road { public: enum LaneCount { TWO_LANE = 2, FOUR_LANE = 4 }; Road(std::string, Coord *, Coord *, LaneCount, Intersection *, Intersection *, int); //shortened 

In particular, in the Road constructor (and any other classes that reference Intersection ). I don't think this is a syntax issue, since Coord is a different class defined in the same way, and the compiler (VS 2008) does not complain about it. This is just Intersection in particular, which gives me this problem.: /

I put his homework - this is what he is for, although this is just a mistake that I can not get rid of, and not part of the problem.

Thoughts?

+7
source share
1 answer

It seems that the error is that you have two header files that circularly include each other - intersection.h and road.h. Doing this tends to lead to strange surprises in C ++ due to how the guards work. For example, suppose I have two header files that look like this:

 // File: Ah #ifndef A_Included #define A_Included #include "Bh" class A {}; void MyFunction(B argument); #endif 

and

 // File: Bh #ifndef B_Included #define B_Included #include "Ah" class B {}; void MyOtherFunction(A argument); #endif 

Now, if I try #include "Ah" , it will expand to

 // File: Ah #ifndef A_Included #define A_Included #include "Bh" class A {}; void MyFunction(B argument); #endif 

When I try to expand #include "Bh" , I get the following:

 // File: Ah #ifndef A_Included #define A_Included // File: Bh #ifndef B_Included #define B_Included #include "Ah" class B {}; void MyOtherFunction(A argument); #endif class A {}; void MyFunction(B argument); #endif 

At this point, the preprocessor will again try to deploy Ah , which will result in the following:

 // File: Ah #ifndef A_Included #define A_Included // File: Bh #ifndef B_Included #define B_Included // File: Ah #ifndef A_Included #define A_Included #include "Bh" class A {}; void MyFunction(B argument); #endif class B {}; void MyOtherFunction(A argument); #endif class A {}; void MyFunction(B argument); #endif 

Now let's see what happens when we allow all these strange guards included. The first time we see A, it expands, as in the case when we expand B for the first time. However, when we see A for the second time, it does not expand at all. Thus, after receiving comments and preprocessor directives, we get the following code:

 class B {}; void MyOtherFunction(A argument); class A {}; void MyFunction(B argument); 

Note that when declaring MyOtherFunction A is not yet declared, and therefore the compiler reports an error.

To fix this, you can redirect declarations A and B to the header files they need:

 // File: Ah #ifndef A_Included #define A_Included class A {}; class B; // Forward declaration void MyFunction(B argument); #endif 

and

 // File: Bh #ifndef B_Included #define B_Included class B {}; class A; // Forward declaration void MyFunction(B argument); #endif 

Now there are no more circular dependencies. As long as you #include corresponding header files in the .cpp files, you should be fine.

Hope this helps!

+25
source

All Articles