I am relatively new to C++, and facing a circular dependency problem. Can someone please help me solve this?
I have two classes:
class Vertex {
s
All you need is to forward-declare the Edge
class before it's used within the Vertex
:
class Edge;
class Vertex {
string name;
int distance;
...
};
class Edge { ... };
You can't put members of type Vertex
instead of the declaration of Vertex
itself, since C++ doesn't allow recursive types. Within the semantics of C++, the size of such type would need to be infinite.
You can, of course, put pointers to Vertex
within Vertex
.
What you want, in fact, within Vertex
's edge and adjacency lists, is pointers, not copies of objects. Thus, your code should be fixed like below (assuming you use C++11, which you in fact should be using now):
class Edge;
class Vertex {
string name;
int distance;
int weight;
bool known;
list> edgeList;
list> adjVertexList;
public:
Vertex();
Vertex(const string & nm);
virtual ~Vertex();
};
class Edge {
Vertex target;
int weight;
public:
Edge();
Edge(const Vertex & v, int w);
virtual ~Edge();
Vertex getTarget();
void setTarget(const Vertex & target);
int getWeight();
void setWeight(int weight);
};