【问题标题】:C++: Two Dimensional Array as class memberC++:二维数组作为类成员
【发布时间】:2013-01-07 19:20:32
【问题描述】:

在我的大学 C++ 课程中,我必须实现一个有向加权图。作为内部表示,我必须实现一个二维数组,它存储有关图中顶点之间的边的信息。

好的,我已经使用重载的 [] 运算符实现了一个 C++ 类“TwoDimArray”。

只要我在 main() 中实例化 TwoDimArray 的对象,它就可以很好地工作。但它不作为类成员。

我的图形表示类是“DirectedGraph”,并且有一个 TwoDimArray* 类型的私有成员“adjacencyMatrix”。

在我的 DirectedGraph 类的构造函数中,我打算最初用零填充数组,表示“节点 i 和 j 之间没有边”。

好的,这就是问题所在。我可以写到坐标 [0][2] (当用 3 个节点初始化图形时,所以数组应该有 3x3 个单元格)。尝试在地址 [1][0] 处写入时,分配操作因分段错误而崩溃。所以赋值操作成功 n 次,失败从 n+1 开始(其中 n 是顶点数)。

任何想法我做错了什么?

我的 TwoDimArray 类(第一个标题,然后是实现):

#ifndef TWODIMARRAY_H_INCLUDED
#define TWODIMARRAY_H_INCLUDED


class TwoDimArray{


 private:


   int* pArr;
   int rows;
   int cols;


 public:

   TwoDimArray(int rows, int cols);
   int* operator[](int row);
   ~TwoDimArray();

};


#endif // TWODIMARRAY_H_INCLUDED

实现:

#include <TwoDimArray.h>

TwoDimArray::TwoDimArray(int nrOfRows, int nrOfCols){

   rows = nrOfRows;
   cols = nrOfCols;

   //allocate memory
   pArr = new int[rows * cols];


 }


int* TwoDimArray::operator [](int row){

   return &pArr[row * cols];
}


  TwoDimArray::~TwoDimArray(){

   delete[] pArr;
}

有向图标题:

    #define DIRECTEDGRAPH_H_INCLUDED
    #include <string>
    #include <list>
    #include <Vertex.h>
    #include <TwoDimArray.h>


    using namespace std;

    /**
     * DOCUMENTATION
     * ======================
     * object oriented Implementation
     * of the abstract
     * Datatype Directed Graph
     * as C++ class
    */

    class DirectedGraph{


       private:

          int maxVertices;
          list<Vertex> vertices;
          TwoDimArray* adjacencyMatrix;
          bool edgeExists(string srcName, string tgtName);
          int vertexExists(string vName);



       public:

          //DirectedGraph();
          DirectedGraph(int maxVertices);
          ~DirectedGraph();


          void AddVertex(Vertex& v);
          void AddEdge(Vertex& source, Vertex& target, int weight);

          int getMaxVertices() const;
          list<Vertex> getVertexNames()const;

          void PrintGraph();

    };




    #endif // DIRECTEDGRAPH_H_INCLUDED

有向图实现(仅构造函数):

    DirectedGraph::DirectedGraph(int maxV){

       this->maxVertices = maxV;

       //initialize the array
       this->adjacencyMatrix = new TwoDimArray(maxV, maxV);

       int i = 0;
       int j = 0;

       for(i = 0; i <= maxVertices - 1; i++){

          for(j = 0; j <= maxVertices - 1; j++){

             // ==> the fatal assignment
             //fails at i = 1 and j = 0

             *adjacencyMatrix[i][j]=0;
             cout << "assigned " << i << " " << j << "with 0"<<endl;
          }
       }
    }

有什么建议吗? 我想将类成员声明为 TwoDimArray* 而不是 TwoDimArray 是不行的,否则它不会编译。

我也尝试过的是:

    DirectedGraph::DirectedGraph(int maxV){

       this->maxVertices = maxV;
               //try to instantiate TwoDimArray 
       TwoDimArray myArr(maxV, maxV);
       this->adjacencyMatrix = myArr;

       int i = 0;
       int j = 0;

       for(i = 0; i <= maxVertices - 1; i++){

          for(j = 0; j <= maxVertices - 1; j++){

             // ==> the fatal assignment
             //fails at i = 1 and j = 0
             myArr[i][j]=0;
             cout << "assigned " << i << " " << j << "with 0"<<endl;
          }
       }
    }

但它同时失败了。 我对 C++ 中的指针逻辑不是很熟悉,我必须承认...

有什么建议吗?

提前致谢 罗兰

【问题讨论】:

  • 请将其缩小到最低限度并修正您的格式 - 不需要双倍行距。

标签: c++ arrays segmentation-fault


【解决方案1】:

您违反了Rule of Three。解决这个问题的最简单方法是避免直接分配内存:

class TwoDimArray{
 private:
   std::vector<int> arr;
   int rows;
   int cols;
 public:
   TwoDimArray(int rows, int cols) : arr(rows * cols);
   int* operator[](int row) { return &arr[cols*row]; }
};

【讨论】:

    【解决方案2】:

    一个问题是您没有为TwoDimArray 提供复制构造函数和赋值运算符。

    这会破坏以下内容:

       TwoDimArray myArr(maxV, maxV);
       this->adjacencyMatrix = myArr;
    

    可能还有其他代码。

    What is The Rule of Three?

    【讨论】:

    • 好的,我明白为什么 this->adjacencyMatrix = myArr 不起作用。我没有实现复制构造函数。但是为什么 *adjacencyMatrix[i][j]=0;不能从 n+1 开始工作?,我的意思是在 main() myArray[i][j] = 0;工作正常...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2022-01-02
    • 2011-01-17
    相关资源
    最近更新 更多