【问题标题】:issue with OOP Class DefinitionsOOP 类定义的问题
【发布时间】:2010-04-30 04:28:40
【问题描述】:

我用 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类中我需要声明遍历类。

我对 declerations 有很大的问题 :) 你能帮帮我吗?

【问题讨论】:

  • 我只是想说,你真的不需要这样评论代码。每一行代码都应该是不言自明的,我认为确实是你的情况,所以你真的不需要复制信息。您应该评论的事情是决定,例如您使用了基于节点对象的图形实现(而不是基于边缘对象或边缘矩阵)。

标签: c++ oop class


【解决方案1】:

前向声明会有所帮助,看看只做 acceptTraverse(Traversal*),Visitor Pattern 可能会对你有所帮助。

【讨论】:

    【解决方案2】:

    如果我理解正确,我试过这个;

    图形类 中,在图形类声明之前我添加了

    class BreadthFirst;
    class DepthFirst;
    

    traversal.cpp 文件中我使用了这个;

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

    有了这个,很多问题都解决了,但是现在;

    错误 8 错误 LNK2001: 未解析的外部符号“公共: 虚拟类 std::basic_string,类 std::allocator > __thiscall Traversal::traverse(类图常量 &)" (?traverse@Traversal@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABVGraph@@@Z)   c:\用户\oben isik\文档\视觉工作室 2010\Projects\hw2\hw2\main.obj

    错误 9 错误 LNK1120: 1 未解决的外部 c:\users\oben isik\文档\视觉工作室 2010\Projects\hw2\Debug\hw2.exe 1

    我现在能做什么?

    【讨论】:

      【解决方案3】:

      不,您不需要类的定义。您只需向编译器提示GraphTraversal 是类。所以在Graph 的定义中使用forward declartion 就像class BreadthFirst; 一样(即在类Graph{..} 之上)。同样在 Traversal 类的定义前使用class Graph;

      【讨论】:

        【解决方案4】:

        在其中一个之前声明你的类,例如在遍历头文件中,在遍历类之前你需要一个声明

        类图;

        那么它就会知道某个时刻会存在一个 Graph 类

        【讨论】:

          猜你喜欢
          • 2011-06-25
          • 1970-01-01
          • 2016-04-15
          • 2014-07-18
          • 1970-01-01
          • 2011-05-07
          • 2011-04-16
          • 1970-01-01
          相关资源
          最近更新 更多