title: C++: Two Dimensional Array as class member
body: <p>in my C++ class at university, i have to implement a Directed, weighted graph. As internal representation i have to implement a two-dimensional array, which stores the information about the edges between the vertices in the graph.</p>
  
  <p>okay, i´ve implemented a C++ class "TwoDimArray" with an overloaded [] operator.</p>
  
  <p>it works well as long as i instantiate objects of TwoDimArray in main(). But it does not as class member.</p>
  
  <p>My Class for representation of the graph is "DirectedGraph" and has a private member "adjacencyMatrix" of Type TwoDimArray*.</p>
  
  <p>In the constructor of my DirectedGraph class i intend to fill the array with zeros initially, indicating "there is no edge between the nodes i and j".</p>
  
  <p>okay, and this is where it all goes wrong. I can write till coordinate [0][2] (when initializing the graph with 3 nodes, so the array should have 3x3 cells). when trying to write at adress [1][0] the assignment operation crashes with segmentation fault. So the assignment operations succeed n times and fail beginning at n+1 (where n is the number of vertices).</p>
  
  <p>Any ideas what i am doing wrong?</p>
  
  <p>My TwoDimArray Class (first header, then implementation):</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>The implementation:</p>
  
  <pre><code>#include &lt;TwoDimArray.h&gt;
  
  TwoDimArray::TwoDimArray(int nrOfRows, int nrOfCols){
  
     rows = nrOfRows;
     cols = nrOfCols;
  
     //allocate memory
     pArr = new int[rows * cols];
  
  
   }
  
  
  int* TwoDimArray::operator [](int row){
  
     return &amp;pArr[row * cols];
  }
  
  
    TwoDimArray::~TwoDimArray(){
  
     delete[] pArr;
  }
  </code></pre>
  
  <p>Directed Graph header:</p>
  
  <pre><code>    #define DIRECTEDGRAPH_H_INCLUDED
      #include &lt;string&gt;
      #include &lt;list&gt;
      #include &lt;Vertex.h&gt;
      #include &lt;TwoDimArray.h&gt;
  
  
      using namespace std;
  
      /**
       * DOCUMENTATION
       * ======================
       * object oriented Implementation
       * of the abstract
       * Datatype Directed Graph
       * as C++ class
      */
  
      class DirectedGraph{
  
  
         private:
  
            int maxVertices;
            list&lt;Vertex&gt; vertices;
            TwoDimArray* adjacencyMatrix;
            bool edgeExists(string srcName, string tgtName);
            int vertexExists(string vName);
  
  
  
         public:
  
            //DirectedGraph();
            DirectedGraph(int maxVertices);
            ~DirectedGraph();
  
  
            void AddVertex(Vertex&amp; v);
            void AddEdge(Vertex&amp; source, Vertex&amp; target, int weight);
  
            int getMaxVertices() const;
            list&lt;Vertex&gt; getVertexNames()const;
  
            void PrintGraph();
  
      };
  
  
  
  
      #endif // DIRECTEDGRAPH_H_INCLUDED
  </code></pre>
  
  <p>Directed Graph Implementation (only the constructor):</p>
  
  <pre><code>    DirectedGraph::DirectedGraph(int maxV){
  
         this-&gt;maxVertices = maxV;
  
         //initialize the array
         this-&gt;adjacencyMatrix = new TwoDimArray(maxV, maxV);
  
         int i = 0;
         int j = 0;
  
         for(i = 0; i &lt;= maxVertices - 1; i++){
  
            for(j = 0; j &lt;= maxVertices - 1; j++){
  
               // ==&gt; the fatal assignment
               //fails at i = 1 and j = 0
  
               *adjacencyMatrix[i][j]=0;
               cout &lt;&lt; "assigned " &lt;&lt; i &lt;&lt; " " &lt;&lt; j &lt;&lt; "with 0"&lt;&lt;endl;
            }
         }
      }
  </code></pre>
  
  <p>any suggestions?
  i guess its not okay to declare the class member as TwoDimArray* instead of TwoDimArray, but otherwise it does not compile.</p>
  
  <p>What i´ve also tried is:</p>
  
  <pre><code>    DirectedGraph::DirectedGraph(int maxV){
  
         this-&gt;maxVertices = maxV;
                 //try to instantiate TwoDimArray 
         TwoDimArray myArr(maxV, maxV);
         this-&gt;adjacencyMatrix = myArr;
  
         int i = 0;
         int j = 0;
  
         for(i = 0; i &lt;= maxVertices - 1; i++){
  
            for(j = 0; j &lt;= maxVertices - 1; j++){
  
               // ==&gt; the fatal assignment
               //fails at i = 1 and j = 0
               myArr[i][j]=0;
               cout &lt;&lt; "assigned " &lt;&lt; i &lt;&lt; " " &lt;&lt; j &lt;&lt; "with 0"&lt;&lt;endl;
            }
         }
      }
  </code></pre>
  
  <p>but it fails at the same point.
  i am not very familiar with pointer logic in c++ i must admit...</p>
  
  <p>any suggestions?</p>
  
  <p>thanks in advance
  Roland</p>