Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that my psychic abilities for fixing your question's code were right, I'd write it like this: </p> <pre><code>struct mine { int rows int columns std::string array_file } void function(const mine&amp; m) { Matrixint array_name(pt-&gt;rows, std::vector&lt;int&gt;(pt-&gt;columns)); std::ifstream infile(m.array_file.c_str()); for (int a = 0; a &lt; ls-&gt;rows &amp;&amp; infile.good(); ++a) { for (int b = 0; b &lt; ls-&gt;columns &amp;&amp; infile.good(); ++b) { infile &gt;&gt; array_name[a][b] ; } } if(!infile) throw "Uh oh!"; // assume real error handling here } </code></pre> <p>Why have I changed all these things? </p> <ul> <li>A class with all public data isn't a <code>class</code>, but an aggregation of data. I'd use a <code>struct</code> for that, to not to confuse those who later need to maintain my code. (That might include me a few years down the road, which is a strong incentive to be very helpful.)</li> <li>Unless you know exactly what you're doing (which doesn't seem to be the case), you should be using <code>std::string</code> rather than C-style strings. </li> <li><a href="https://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/">Why pass the function parameter by pointer, when you can use a <code>const</code> reference</a>? </li> <li><code>std::ifstream</code> has a constructor with which you can open a file immediately. I rarely ever use (or see used) its <code>open()</code> member function. </li> <li>You need to test whether the file was opened and whether the input operations succeeded. (In this case, I merged the test whether it could be opened with the input operation succeed test, since the for loop is a pre-testing loop.) After the operations, I check whether an error occurred. Alternatively you could break out of the loop with an exception when reading fails. </li> <li>Usually there is no need to close a stream, since its destructor already does this. If you make your variables as local as possible (which is good programming praxis), the file will be closed right away nevertheless. </li> </ul>
    singulars
    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