2010-04-30 51 views
3

我在C++中爲我的作業工作,並且在乘法定義方面存在一些問題。與OOP類定義問題

我的圖課;

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類中,圖類,我需要遍歷類來聲明。

我遇到了很多問題:)請幫助我嗎?

+1

我只想說,你真的不需要這樣評論代碼。每一行代碼都應該是不言自明的,我認爲它確實適合您的情況,所以您實際上不需要複製這些信息。您應該評論的內容是決定,例如您使用基於節點對象的圖形實現(而不是基於邊緣對象或邊緣矩陣)。 – 2010-04-30 09:12:07

回答

0

不,你不需要類的定義。你只需要給編譯器提示GraphTraversal是類。因此,在Graph的定義中使用forward declartion,如class BreadthFirst;(我在類Graph {....}之上)。在定義Traversal類之前同樣使用class Graph;

0

前他們中的一個,如之前聲明你的類,在橫越頭文件,遍歷類,你需要一份聲明

類圖之前;

那麼它會知道一個Graph類將在某個時候

2

存在正向宣佈將幫助,考慮只是做acceptTraverse(遍歷*),該Visitor Pattern的東西,可以幫助你。

1

如果我真的理解了,我試過了;在圖類,之前圖類declerations我增加

class BreadthFirst; 
class DepthFirst; 

traversal.cpp文件我用這個

;

#include "Graph.h" 
#include "Traversal.h" 

有了這個,很多問題都解決了,但現在;

錯誤8錯誤LNK2001: 解析的外部符號 「公共: 虛擬類 的std :: basic_string的,類 的std ::分配器> __thiscall 遍歷::遍歷(類格拉夫常量 &)」 ( ?traverse @ Traversal @@ UAE?AV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ ABVGraph @@@ Z) & nbsp; C:\用戶\ OBEN Isik的\文檔\ Visual工作室 2010 \項目\ HW2 \ HW2 \ main.obj

錯誤9錯誤LNK1120:1個 無法解析的外部C:\用戶\ OBEN Isik的\文檔\ Visual studio 2010 \ Projects \ hw2 \ Debug \ hw2.exe 1

我現在能做什麼?