导致C2061的用户创建头:语法错误:标识符“classname”

时间:2022-07-31 22:44:43

So, I sort of expect this to end up being a simple answer, but I've been hacking at it for a while now, and can't seem to fix this issue. So I have a particular class, Intersection, that when included in any other header gives me:

所以,我有点期望这个结果会是一个简单的答案,但我已经钻研了一段时间,似乎无法解决这个问题。所以我有一个特殊的类,交集,当它包含在任何其他header中时,我得到:

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 attempt to use this class elsewhere, I get the 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

Particularly at the Road constructor (and any other classes which reference Intersection). I don't think it's a syntax problem, as Coord is another class, defined in the same manner, and the compiler (VS 2008) doesn't complain about it. It's just Intersection in particular that's giving me this trouble. :/

特别是在道路构造函数(和任何其他类中引用交叉)。我不认为这是一个语法问题,因为Coord是另一个以相同方式定义的类,编译器(VS 2008)也没有抱怨它。它只是交点,这给我带来了麻烦。:/

I'm tagging it homework -- it's what it's for, even though this is just an error I can't get rid of rather than part of the problem.

我给它贴上作业标签——这就是它的目的,尽管这只是一个我无法摆脱的错误,而不是问题的一部分。

Thoughts?

想法吗?

1 个解决方案

#1


25  

It looks like the error is that you have two header files that are circularly including one another - intersection.h and road.h. Doing this tends to lead to weird surprises in C++ because of how include guards work. For example, suppose that I have two header files that look like this:

看起来错误在于你有两个头文件,它们循环地包含着另一个-交集。h和road.h。这样做会在c++中带来奇怪的惊喜,因为如何包含警卫。例如,假设我有两个头文件看起来像这样:

// File: A.h
#ifndef A_Included
#define A_Included

#include "B.h"

class A {};

void MyFunction(B argument);
#endif

and

// File: B.h
#ifndef B_Included
#define B_Included

#include "A.h"

class B {};

void MyOtherFunction(A argument);
#endif

Now, if I try to #include "A.h", then it expands out to

现在,如果我尝试#include "A。h",然后膨胀到

// File: A.h
#ifndef A_Included
#define A_Included

#include "B.h"

class A {};

void MyFunction(B argument);
#endif

When I try expanding out the #include "B.h", I get this:

当我尝试扩展#include时,“B”。h”,我得到了这个:

// File: A.h
#ifndef A_Included
#define A_Included

// File: B.h
#ifndef B_Included
#define B_Included

#include "A.h"

class B {};

void MyOtherFunction(A argument);
#endif

class A {};

void MyFunction(B argument);
#endif

At this point, the preprocessor will again try expanding out A.h, which leads to this:

在这一点上,预处理器将再次尝试扩展A。h,由此得出:

// File: A.h
#ifndef A_Included
#define A_Included

// File: B.h
#ifndef B_Included
#define B_Included

// File: A.h
#ifndef A_Included
#define A_Included

#include "B.h"

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 resolve all of these weird include guards. The first time we see A, it's expanded out, as is the case when we expand out B for the first time. However, when we see A for the second time, it's not expanded out at all. Thus, after taking out comments and preprocessor directives, we get this resulting code:

现在,让我们看看当我们解决所有这些奇怪的包括守卫的时候会发生什么。第一次看到A,它是展开的,就像我们第一次展开B的情况一样。然而,当我们第二次看到A时,它并没有膨胀。因此,在去掉注释和预处理指令之后,我们得到了以下代码:

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

Notice that when MyOtherFunction is declared, A has not yet been declared, and so the compiler reports an error.

注意,当声明MyOtherFunction时,还没有声明A,因此编译器报告错误。

To fix this, you can forward-declare A and B in the header files that need them:

要解决这个问题,您可以在需要它们的头文件中向前声明A和B:

// File: A.h
#ifndef A_Included
#define A_Included

class A {};
class B;    // Forward declaration

void MyFunction(B argument);
#endif

and

// File: B.h
#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 the appropriate header files in the .cpp files, you should be fine.

现在,不再有循环依赖了。只要您在.cpp文件中包含适当的头文件,您就应该没问题。

Hope this helps!

希望这可以帮助!

#1


25  

It looks like the error is that you have two header files that are circularly including one another - intersection.h and road.h. Doing this tends to lead to weird surprises in C++ because of how include guards work. For example, suppose that I have two header files that look like this:

看起来错误在于你有两个头文件,它们循环地包含着另一个-交集。h和road.h。这样做会在c++中带来奇怪的惊喜,因为如何包含警卫。例如,假设我有两个头文件看起来像这样:

// File: A.h
#ifndef A_Included
#define A_Included

#include "B.h"

class A {};

void MyFunction(B argument);
#endif

and

// File: B.h
#ifndef B_Included
#define B_Included

#include "A.h"

class B {};

void MyOtherFunction(A argument);
#endif

Now, if I try to #include "A.h", then it expands out to

现在,如果我尝试#include "A。h",然后膨胀到

// File: A.h
#ifndef A_Included
#define A_Included

#include "B.h"

class A {};

void MyFunction(B argument);
#endif

When I try expanding out the #include "B.h", I get this:

当我尝试扩展#include时,“B”。h”,我得到了这个:

// File: A.h
#ifndef A_Included
#define A_Included

// File: B.h
#ifndef B_Included
#define B_Included

#include "A.h"

class B {};

void MyOtherFunction(A argument);
#endif

class A {};

void MyFunction(B argument);
#endif

At this point, the preprocessor will again try expanding out A.h, which leads to this:

在这一点上,预处理器将再次尝试扩展A。h,由此得出:

// File: A.h
#ifndef A_Included
#define A_Included

// File: B.h
#ifndef B_Included
#define B_Included

// File: A.h
#ifndef A_Included
#define A_Included

#include "B.h"

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 resolve all of these weird include guards. The first time we see A, it's expanded out, as is the case when we expand out B for the first time. However, when we see A for the second time, it's not expanded out at all. Thus, after taking out comments and preprocessor directives, we get this resulting code:

现在,让我们看看当我们解决所有这些奇怪的包括守卫的时候会发生什么。第一次看到A,它是展开的,就像我们第一次展开B的情况一样。然而,当我们第二次看到A时,它并没有膨胀。因此,在去掉注释和预处理指令之后,我们得到了以下代码:

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

Notice that when MyOtherFunction is declared, A has not yet been declared, and so the compiler reports an error.

注意,当声明MyOtherFunction时,还没有声明A,因此编译器报告错误。

To fix this, you can forward-declare A and B in the header files that need them:

要解决这个问题,您可以在需要它们的头文件中向前声明A和B:

// File: A.h
#ifndef A_Included
#define A_Included

class A {};
class B;    // Forward declaration

void MyFunction(B argument);
#endif

and

// File: B.h
#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 the appropriate header files in the .cpp files, you should be fine.

现在,不再有循环依赖了。只要您在.cpp文件中包含适当的头文件,您就应该没问题。

Hope this helps!

希望这可以帮助!