title: Segmentation fault in overloaded operator>>
body: <p>I'm trying to write code for an orthogonal linked sparse matrix.  </p>
  
  <p>Here's the question:<br>
  An alternative linked representation for sparse matrices uses nodes that have the fields down, right, row, col, and value. Each non-zero entry of the sparse matrix is represented
  by a node. The zero terms are not explicitly stored. The nodes are linked together to form two circular lists. The rst list, the row list, is made up by linking nodes by rows and within rows by columns using the right field. The second,list, the column list, is made up by linking nodes via the down field. In this list, nodes are linked by columns and within columns by rows. These two lists share a common header node. In addition, a node is added to the dimensions of the matrix.  </p>
  
  <p>The input file looks like this:  </p>
  
  <p>// Matrix A  </p>
  
  <p>4   4   7<br>
  1   1   2<br>
  1   4   1<br>
  2   2   7<br>
  3   1   9<br>
  3   3   8<br>
  4   2   4<br>
  4   3   5</p>
  
  <p>// Matrix B  </p>
  
  <p>4   4   5<br>
  1   3   4<br>
  2   1   6<br>
  2   3   3<br>
  3   2   5<br>
  4   4   9    </p>
  
  <p>This is my code for the operator>>:</p>
  
  <pre><code>istream&amp; operator&gt;&gt;(istream&amp; in, OrthogonalLinkedSparseMatrix&amp; x){
  
      in &gt;&gt; x.numRows &gt;&gt; x.numCols &gt;&gt; x.numTerms;
      in &gt;&gt; x.currentNode-&gt;row &gt;&gt; x.currentNode-&gt;col &gt;&gt; x.currentNode-&gt;value;
      x.push_back(x.currentNode);
      if((x.currentNode-&gt;row == 1)&amp;&amp;(x.currentNode-&gt;col == 1)){
  
          x.hnode-&gt;right = x.currentNode;
          x.hnode-&gt;down = x.currentNode;
      }
      if(x.currentNode-&gt;col == 1){
          x.hnode-&gt;down = x.currentNode;
      }
      if(x.currentNode-&gt;row == 1){
          x.hnode-&gt;right = x.currentNode;
      }
  
      for (int i = 2; i &lt;= x.numTerms; i++) {
  
          in &gt;&gt; x.currentNode-&gt;row &gt;&gt; x.currentNode-&gt;col &gt;&gt; x.currentNode-&gt;value;
  
          x.push_back(x.currentNode);
  
      }
  
  
      return in;
  
  }
  </code></pre>
  
  <p>It compiles fine. But when I try running it, I keep getting a segmentation fault error.<br>
  Can anyone help??
  Thanks a bunch!  </p>
  
  <p>Here's OrthogonalLinkedSparseMatrix.h:  </p>
  
  <pre><code>#ifndef O_L_SPARSE_MATRIX_H
  
  #define O_L_SPARSE_MATRIX_H
  
  
  
  #include &lt;iostream&gt;
  
  #include &lt;fstream&gt;
  
  #include "node.h"
  
  #include "myExceptions.h"
  
  
  
  using namespace std;
  
  
  
  class OrthogonalLinkedSparseMatrix;
  
  ostream&amp; operator&lt;&lt; (ostream&amp;, OrthogonalLinkedSparseMatrix&amp;);
  
  istream&amp; operator&gt;&gt; (istream&amp;, OrthogonalLinkedSparseMatrix&amp;);
  
  
  
  class OrthogonalLinkedSparseMatrix{
  
  public:
  
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, OrthogonalLinkedSparseMatrix&amp; x);
  
      friend istream&amp; operator&gt;&gt;(istream&amp; in, OrthogonalLinkedSparseMatrix&amp; x);
  
      OrthogonalLinkedSparseMatrix(){}
  
      ~OrthogonalLinkedSparseMatrix(){}
  
      void transpose(OrthogonalLinkedSparseMatrix &amp;b);
  
      void add(OrthogonalLinkedSparseMatrix &amp;a, OrthogonalLinkedSparseMatrix &amp;c);
  
      void push_back(matrixNode *&amp;mat_Node);
  
      void setDowns(matrixNode *&amp;mat_Node);
  
  private:
  
      matrixNode *hnode;
  
      int numRows, numCols, numTerms;
  
      matrixNode *currentNode;
  
      matrixNode *previousNode;
  
      matrixNode *nextNode;
  
  };
  
   // code for operator &gt;&gt; &amp; &lt;&lt;, etc goes here, but everything's commented out except operator &gt;&gt;  
  
  #endif
  </code></pre>
  
  <p>EDIT: I'm including operator&lt;&lt; as well:  </p>
  
  <pre><code>    ostream&amp; operator&lt;&lt;(ostream&amp; out, OrthogonalLinkedSparseMatrix&amp; x){
  
      if(x.numTerms == 0){
  
          out &lt;&lt; "No non-zero terms" &lt;&lt; endl;
  
          return out;
  
      }
  
      out &lt;&lt; x.numRows &lt;&lt; x.numCols &lt;&lt; x.numTerms &lt;&lt; endl;
  
      for (int i = 0; i &lt; x.numTerms; i++) {
  
          out &lt;&lt; x.currentNode-&gt;row &lt;&lt; x.currentNode-&gt;col &lt;&lt; x.currentNode-&gt;value &lt;&lt; endl;
  
      }
  
      return out;
  
  }
  </code></pre>