大学の C++ クラスで、有向加重グラフを実装する必要があります。内部表現として、グラフの頂点間のエッジに関する情報を格納する 2 次元配列を実装する必要があります。
わかりました、オーバーロードされた [] 演算子を使用して 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 ++のポインターロジックにあまり慣れていないことを認めなければなりません...
助言がありますか?
前もってありがとうローランド