Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’
    primarykey
    data
    text
    <p>I have a class that uses a nested class, and want to use the nested class <code>operator&lt;&lt;</code> to define <code>operator&lt;&lt;</code> in the upper class. Here is how my code looks like:</p> <pre><code>#include &lt;memory&gt; #include &lt;iostream&gt; template&lt;typename T&gt; struct classA { struct classB { template&lt;typename U&gt; friend inline std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, const typename classA&lt;U&gt;::classB &amp;b); }; classB root; template&lt;typename U&gt; friend std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, const classA&lt;U&gt; &amp;tree); }; template&lt;typename T&gt; inline std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, const classA&lt;T&gt; &amp;tree) { out &lt;&lt; tree.root; return out; } template&lt;typename T&gt; inline std::ostream&amp; operator&lt;&lt; (std::ostream &amp;out, const typename classA&lt;T&gt;::classB &amp;b) { return out; } int main() { classA&lt;int&gt; a; std::cout &lt;&lt; a; } </code></pre> <ul> <li><p>When compiling without support for C++11, the definition of operator&lt;&lt; for the inner class seems not to be found by the compiler:</p> <pre><code>so.hpp:24:7: error: no match for ‘operator&lt;&lt;’ in ‘out &lt;&lt; tree.classA&lt;int&gt;::root’ so.hpp:24:7: note: candidates are: ... </code></pre></li> <li><p>With GCC 4.6 and 4.7 when compiling with std=c++0x:</p> <pre><code>so.hpp:21:3: error: cannot bind ‘std::ostream {aka std::basic_ostream&lt;char&gt;}’ lvalue to ‘std::basic_ostream&lt;char&gt;&amp;&amp;’ In file included from /usr/include/c++/4.7/iostream:40:0, from so.hpp:2: /usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of ‘std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;&amp;, const _Tp&amp;) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; _Tp = classA&lt;int&gt;::classB]’ </code></pre></li> </ul> <p>Can someone tell me why this code is not legal, and what's the best way to do what I want?</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