Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After fixing up your program, and with the hint from @user763305, I suppose you have something like this (note: this program compiles, but invokes undefined behavior when run, since the <code>storage</code> vector is left empty):</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; class csv_File { public: csv_File(std::string) {} std::string const&amp; access(int,int) const; void modify_element(int column,int row ,std::string value) { storage.at(row).at(column)=value; } private: mutable std::vector &lt; std::vector&lt;std::string&gt; &gt; storage; }; std::string const&amp; csv_File::access(int column,int row) const { return storage.at(row).at(column); } const csv_File&amp; Factory() { static csv_File foo("foo.txt"); return foo; } int main(){ const csv_File&amp; f(Factory()); f.modify_element(1, 2, "hello"); } </code></pre> <p>and you get an error like this:</p> <pre><code>cs.cc: In function ‘int main()’: cs.cc:24: error: passing ‘const csv_File’ as ‘this’ argument of ‘void csv_File::modify_element(int, int, std::string)’ discards qualifiers </code></pre> <p>So, you have a <code>const</code> object and are trying to invoke <code>modify_element</code> on it. Logically, this is a mistake -- you can't modify <code>const</code> objects!</p> <p>You have, as I see it, two choices for a fix. Either you can declare the <code>vector</code> object <code>mutable</code> and the <code>modify_element</code> method <code>const</code>, or you can modify your object factory to return non-const references.</p> <p>Solution 1:</p> <pre><code>... void modify_element(int column,int row ,std::string value) const { storage.at(row).at(column)=value; } ... </code></pre> <p>Solution 2:</p> <pre><code>... const csv_File&amp; Factory() { static csv_File foo("foo.txt"); return foo; } csv_File&amp; RW_Factory() { static csv_File foo("foo.txt"); return foo; } ... const csv_File&amp; f(RW_Factory()); ... </code></pre> <p><hr /> <strong><em>EDIT</em></strong> And, yes, if you previously returned a reference to a <code>string</code> in your <code>vector</code>, and you subsequently assign a new value to that <code>string</code> as you do in <code>modify_element</code>, then the previous reference will reflect the new value.</p> <p>Note that the previous reference will be invalid if you destroy the vector, erase that <code>vector</code> element, or do anything to cause the <code>vector</code> to grow beyond its previous capacity.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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