Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe that the problem with your <code>operator &gt;&gt;</code> is that you're using whatever dimensions already happen to be in the <code>Matrix</code> rather than trying to recover the dimensions from the input that you find.</p> <p>I think that your best bet would be to have the <code>operator &lt;&lt;</code> implementation preface the matrix with dimension information (such as the number of rows and columns) and then have the <code>operator &gt;&gt;</code> function read in that information. For example:</p> <pre><code>ostream &amp;operator&lt;&lt;(ostream &amp;out, const Matrix &amp;m) { out &lt;&lt; m.dx &lt;&lt; ' ' &lt;&lt; out.dy &lt;&lt; '\n'; for (int i = 0; i &lt; m.dx; ++i) { for (int j = 0; j &lt; m.dy; ++j) out &lt;&lt; m.p[i][j] &lt;&lt; " "; out &lt;&lt; endl; } return out; } </code></pre> <p>With this in hand, you can write your stream extraction operator as</p> <pre><code>istream &amp;operator&gt;&gt;(istream &amp;in, Matrix &amp;m) { in &gt;&gt; m.dx &gt;&gt; m.dy; /* Some sort of logic to ensure that you've allocated an array large enough to * hold all the elements ... */ for (int i = 0; i &lt; m.dx; ++i) { for (int j = 0; j &lt; m.dy; ++j) in &gt;&gt; m.p[i][j]; } return in; } </code></pre> <p>This may not be the most aesthetically pleasing input and output operators, but they should get the job done.</p> <p>If you want to make these operators a bit classier, consider outputting the elements of the matrix using some special character to delimit rows and columns. For example, you might try outputting the matrix</p> <pre><code>0 1 2 3 4 5 6 7 8 </code></pre> <p>as</p> <pre><code>[[0 1 2][3 4 5][6 7 8]] </code></pre> <p>With this setup, the size information about the matrix is implicit in how the bracket grouping works. Then again, this may make it a bit trickier to read the input, since you wouldn't know in advance how large the matrix is. But go with what's easiest for yourself overall.</p> <p>As an FYI, you probably don't want to use <code>endl</code> to delimit lines when writing a stream insertion operator. In addition to writing a newline, <code>endl</code> flushes the buffer. If your stream is hooked up to a network connection, you may not want to keep flushing the buffer whenever you have a new line of the matrix, since that could result in a lot of data getting sent in bursts (slow) rather than grouping it all together at once (fast).</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload