Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is g++ saying 'no match for ‘operator=’ when there clearly is, and Visual Studio can see that there is?
    primarykey
    data
    text
    <p>I'm writing an interface library that allows access to variables within tables (up to a theoretically infinite depth) in an object of type <code>regula::State</code>. I'm accomplishing this by overloading <code>operator[]</code> within a class, which then returns another of that same class, and calls <code>operator[]</code> again as needed. For example:</p> <pre><code>regula::State t; t["math"]["pi"] = 3.14159; </code></pre> <p>The above is supposed to place the value <code>3.14159</code> within variable <code>pi</code> in table <code>math</code>. Basically, it does this by have <code>t</code> return a proxy object representing <code>math</code>, which returns another proxy object representing <code>pi</code>, to which we actually save the variable. The internals of this aren't really relevant to the question, but here is the function header.</p> <pre><code>LObject LObject::operator[] (const std::string name); </code></pre> <p>Basically, in the example above, the program should call <code>t</code>'s <code>operator[]</code> with the string <code>"math"</code> and return another object, and then call that object's <code>operator[]</code> with the string <code>"pi"</code>, which returns the final object, and then assigns the value to that one using <code>operator=</code>.</p> <pre><code>template &lt;typename T&gt; T LObject::operator= (const T&amp; value); </code></pre> <p>The <code>T</code> returned is just a copy of the <code>value</code> passed.</p> <p>Now, my code produces NO errors in Visual C++ 2008 and works perfectly. But when I try to compile it on Linux with <code>g++</code>, I get the following error:</p> <pre><code>../../test/regula-test.cpp:100: error: no match for ‘operator=’ in ‘L.regula::State::operator[](std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(((const char*)"Numbers"), ((const std::allocator&lt;char&gt;&amp;)((const std::allocator&lt;char&gt;*)(&amp; std::allocator&lt;char&gt;()))))) = Numbers’ ../../include/regula.hpp:855: note: candidates are: regula::LObject&amp; regula::LObject::operator=(const regula::LObject&amp;) </code></pre> <p>For some reason, <code>g++</code> seems to be trying to call <code>operator=</code> on <code>operator[]</code>, rather than on the returned object like it is supposed to be.</p> <p>I can actually fix this error by replacing the return type on <code>operator=</code> with <code>void</code>:</p> <pre><code>template &lt;typename T&gt; /*T*/ void LObject::operator= (const T&amp; value); </code></pre> <p>But this is not preferable, and besides, I have similar errors in several other locations with a similarly overloaded <code>operator==</code>:</p> <pre><code>../../test/regula-test.cpp:153: error: no match for ‘operator==’ in ‘pi == L.regula::State::operator[](std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(((const char*)"pi"), ((const std::allocator&lt;char&gt;&amp;)((const std::allocator&lt;char&gt;*)(&amp; std::allocator&lt;char&gt;())))))’ </code></pre> <p>I don't understand why this error is occurring in g++, or why it is not occurring in Visual C++. Can anyone shed any light on this or recommend any solutions?</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.
 

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