Note that there are some explanatory texts on larger screens.

plurals
  1. POValues not written to vector
    primarykey
    data
    text
    <p>I'm trying to read pairs values from a file in the constructor of an object.</p> <p>The file looks like this:</p> <pre><code>4 1 1 2 2 3 3 4 4 </code></pre> <p>The first number is number of pairs to read.</p> <p>In some of the lines the values seem to have been correctly written into the vector. In the next they are gone. I am totally confused</p> <pre><code>inline BaseInterpolator::BaseInterpolator(std::string data_file_name) { std::ifstream in_file(data_file_name); if (!in_file) { std::cerr &lt;&lt; "Can't open input file " &lt;&lt; data_file_name &lt;&lt; std::endl; exit(EXIT_FAILURE); } size_t n; in_file &gt;&gt; n; xs_.reserve(n); ys_.reserve(n); size_t i = 0; while(in_file &gt;&gt; xs_[i] &gt;&gt; ys_[i]) { // this line prints correct values i.e. 1 1, 2 2, 3 3, 4 4 std::cout &lt;&lt; xs_[i] &lt;&lt; " " &lt;&lt; ys_[i] &lt;&lt; std::endl; // this lines prints xs_.size() = 0 std::cout &lt;&lt; "xs_.size() = " &lt;&lt; xs_.size() &lt;&lt; std::endl; if(i + 1 &lt; n) i += 1; else break; // this line prints 0 0, 0 0, 0 0 std::cout &lt;&lt; xs_[i] &lt;&lt; " " &lt;&lt; ys_[i] &lt;&lt; std::endl; } // this line prints correct values i.e. 4 4 std::cout &lt;&lt; xs_[i] &lt;&lt; " " &lt;&lt; ys_[i] &lt;&lt; std::endl; // this lines prints xs_.size() = 0 std::cout &lt;&lt; "xs_.size() = " &lt;&lt; xs_.size() &lt;&lt; std::endl; } </code></pre> <p>The class is defined thus:</p> <pre><code>class BaseInterpolator { public: ~BaseInterpolator(); BaseInterpolator(); BaseInterpolator(std::vector&lt;double&gt; &amp;xs, std::vector&lt;double&gt; &amp;ys); BaseInterpolator(std::string data_file_name); virtual int interpolate(std::vector&lt;double&gt; &amp;x, std::vector&lt;double&gt; &amp;fx) = 0; virtual int interpolate(std::string input_file_name, std::string output_file_name) = 0; protected: std::vector&lt;double&gt; xs_; std::vector&lt;double&gt; ys_; }; </code></pre>
    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.
 

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