我在C++中从事家庭作业,并且乘法定义存在一些问题。
我的图类;
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 *);
};
和我的遍历类;
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();
};
我的问题是在遍历类中,我需要同时声明Graph类,在图类中我需要遍历类来声明。
我在脱节方面有很大的问题:)能请你帮帮我吗?
最佳答案
不,您不需要类的定义。您只需要向编译器提示Graph
和Traversal
是类。因此,请在class BreadthFirst;
的定义中使用类似于Graph
的forward declartion(即,在Graph {....}类上方)。类似地,在遍历类的定义之前使用class Graph;
。