c++编译器错误类没有名为的成员
我有一个名为 Edge 的类和一个名为 Vertex 的类,
在我的 Edge 类中,有一个名为 target 的 Vertex 的引用。在 Vertex 中,我发送一个 Edge 并尝试通过 Edge 更改目标,但出现编译器错误,类 Edge 没有名为 target 的成员。
我的 Edge.h 是
#include "Vertex.h"
class Edge
{
public:
Edge *data;
Edge *next;
Vertex *target;
Edge();
Edge(Edge *x);
Edge(Vertex *x);
void print();
};
由 Vertex.cpp 中的这段代码引起的错误,
Vertex::Vertex(Edge *x)
{
name = x->target->name;
next = x->target->next;
mark = x->target->mark;
previous = NULL;
next = NULL;
}
当我尝试编译 Vertex 时,确切的错误是
g++ -g -I. -c -o Vertex.o Vertex.cpp
In file included from Vertex.h:3,
from Vertex.cpp:3:
Edge.h:10: error: ISO C++ forbids declaration of ‘Vertex’ with no type
Edge.h:10: error: expected ‘;’ before ‘*’ token
Edge.h:14: error: expected ‘)’ before ‘*’ token
Vertex.cpp: In constructor ‘Vertex::Vertex(Edge*)’:
Vertex.cpp:26: error: ‘class Edge’ has no member named ‘target’
Vertex.cpp:27: error: ‘class Edge’ has no member named ‘target’
Vertex.cpp:28: error: ‘class Edge’ has no member named ‘target’
I have a class called Edge and a class called Vertex
in my Edge class there is a reference to a Vertex called target. in Vertex I send in an Edge and try to change the target through the Edge but am having a compiler error of class Edge has no member named target.
my Edge.h is
#include "Vertex.h"
class Edge
{
public:
Edge *data;
Edge *next;
Vertex *target;
Edge();
Edge(Edge *x);
Edge(Vertex *x);
void print();
};
the error is caused by this code in Vertex.cpp
Vertex::Vertex(Edge *x)
{
name = x->target->name;
next = x->target->next;
mark = x->target->mark;
previous = NULL;
next = NULL;
}
the exact error when i try to compile Vertex is
g++ -g -I. -c -o Vertex.o Vertex.cpp
In file included from Vertex.h:3,
from Vertex.cpp:3:
Edge.h:10: error: ISO C++ forbids declaration of ‘Vertex’ with no type
Edge.h:10: error: expected ‘;’ before ‘*’ token
Edge.h:14: error: expected ‘)’ before ‘*’ token
Vertex.cpp: In constructor ‘Vertex::Vertex(Edge*)’:
Vertex.cpp:26: error: ‘class Edge’ has no member named ‘target’
Vertex.cpp:27: error: ‘class Edge’ has no member named ‘target’
Vertex.cpp:28: error: ‘class Edge’ has no member named ‘target’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我正确理解情况,似乎您在 .cpp 中具有 Edge 的类声明,而不是 .h (那么标头中是什么?)。 Vertex 中出现错误的原因是,当编译器查找 Edge 类时,它找不到 Edge 标头中的声明,换句话说,它是隐藏的。您的 Edge 类声明应位于头文件中,定义应位于 .cpp 中。另请注意,这是循环依赖的一个很好的例子,这通常会导致痛苦。看看你能不能打破它们。
编辑:感谢您将确切的错误放在那里,它几乎证实了我们所有的猜测。确保两个类可以互相看到——确保 Vertex 包含 Edge 的标头。如果类足够小,您可能希望将它们转储到一个文件中,正如 Falmarri 建议的那样。另外,不要忘记使用前向声明来解决这些类型的问题循环依赖。如果您在类中包含对外部类的指针或引用,则可以转发声明,但它不适用于类中的实际对象(例如
Edge edge;
)。我认为原因是指针和引用只是地址,因此编译器不需要了解内部结构,但要使用实际对象,您必须知道内部内容。If I understand the situation correctly, it seems you have the class declaration for Edge in the .cpp, rather than the .h (what's in the header then?). The error in Vertex arises because when the compiler looks up the Edge class, it can't find the declaration in the Edge header--in other words, it's hidden. Your Edge class declaration should be in the header file and the definition should be in the .cpp. Note also that this is a nice case of circular dependencies, which can often lead to pain. See if you can't break them.
Edit: Thanks for putting the exact error up there, it pretty much confirmed all our guesses. Make sure that both classes can see each other--ensure that Vertex includes Edge's header. If the classes are small enough, you might want to dump them both in one file, as Falmarri suggested. Also, don't forget about using forward declarations to solve these types of circular dependencies. You can forward declare if you include pointers or references to the external class in your class, but it doesn't work with actual objects (like
Edge edge;
) in your class. I believe the reason for this is that pointers and refs are just addresses, so the compiler doesn't need to know about the internals, but to use an actual object you have to know what's inside.所有其他答案都正确地解释了两个类之间的循环依赖问题,并解释了如何解决它。
我的建议是让 Vertex 类不知道 Edge 类。只需将 getTargetVertex() 和 getSourceVertex() 方法添加到边缘类,并仅使用 Vertex 类中的复制构造函数即可。
当然,如果不检查可用的边池/边列表中的每个边实例,此解决方案将很难知道以顶点为目标的边是什么。
All the other answers correctly explain that you have a problem with the circular dependency between the two classes and explain how to solve it.
My suggestion is to make the Vertex class unaware of the Edge class. Simply by adding the getTargetVertex() and getSourceVertex() methods to the edge class and using only the copy constructor of in the Vertex class.
Of course, this solution will make it difficult to know what are the edges that target a vertex without checking every edge instance in your available pool/list of edges.
将 Edge 声明放在 Edge.h 中,并将 Edge.h 包含在 Vertex.cpp 中。
Put Edge declaration in Edge.h and include Edge.h in Vertex.cpp.
看来你有循环依赖。您可以通过在
Edge
声明中未声明的类名(例如class Vertex *target;
)之前添加class
来修复此问题。Seems you have a cyclic dependencies. You could fix it by adding
class
before the undeclared class names likeclass Vertex *target;
in yourEdge
declaration.