I work for my homework in C++ and i have some problems with multiply definitions.
My graph class ;
class Graph{
private:
string name; //Graph name
fstream* graphFile; //Graph's file
protected:
string opBuf; //Operations buffer
int containsNode(string); //Query if a node is present
Node* nodes; //Nodes in the graph
int nofNodes; //Number of nodes in the graph
public:
static int nOfGraphs; //Number of graphs produced
Graph(); //Constructors and destructor
Graph(int);
Graph(string);
Graph(const Graph &);
~Graph();
string getGraphName(); //Get graph name
bool addNode(string); //add a node to the graph
bool deleteNode(string); //delete a node from the graph
bool addEdge(string,string); //add an edge to the graph
bool deleteEdge(string,string); //delete an edge from the graph
void intersect(const Graph&); //intersect the graph with the <par>
void unite(const Graph&); //intersect the graph with the <par>
string toString(); //get string representation of the graph
void acceptTraverse(BreadthFirst*);
void acceptTraverse(DepthFirst *);
};
and my traversal class;
class Traversal {
public:
string *visitedNodes;
virtual string traverse (const Graph & );
};
class BreadthFirst : public Traversal {
public :
BreadthFirst();
string traverse();
};
class DepthFirst : public Traversal {
public :
DepthFirst();
string traverse();
};
My problem is in traversal class , i need to declare Graph class at the same time , in graph class i need traversal class to dec开发者_StackOverflowlare.
I have big problems with declerations :) Could you please help me ?
Forward declaring would help, look into just doing acceptTraverse(Traversal*), the Visitor Pattern is something that might help you.
if i understood truely, i tried this ;
in graph class , before graph class declerations i added
class BreadthFirst;
class DepthFirst;
and in traversal.cpp file i used this ;
#include "Graph.h"
#include "Traversal.h"
With this, lots of problems solved but for now ;
Error 8 error LNK2001: unresolved external symbol "public: virtual class std::basic_string,class std::allocator > __thiscall Traversal::traverse(class Graph const &)" (?traverse@Traversal@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABVGraph@@@Z) & nbsp; c:\Users\oben isik\documents\visual studio 2010\Projects\hw2\hw2\main.obj
Error 9 error LNK1120: 1 unresolved externals c:\users\oben isik\documents\visual studio 2010\Projects\hw2\Debug\hw2.exe 1
What can i do now ?
No you don't need the definition of the classes. You just need to give the hint to the compiler that Graph
and Traversal
are classes. So use forward declartion like class BreadthFirst;
in the definition of Graph
(i..e just above class Graph{....}). Similarly use class Graph;
before the definition of Traversal class.
pre declare your class before one of them, eg, in the Traversal header file, before the traversal class you need a statement
class Graph;
then it will know a Graph class will exist at some point
精彩评论