开发者

c++ compiler error class has no member named

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 ‘tar开发者_运维知识库get’
Vertex.cpp:28: error: ‘class Edge’ has no member named ‘target’


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.


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.


Put Edge declaration in Edge.h and include Edge.h in Vertex.cpp.


Seems you have a cyclic dependencies. You could fix it by adding class before the undeclared class names like class Vertex *target; in your Edge declaration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜