Note that there are some explanatory texts on larger screens.

plurals
  1. POgcc failes to compile operator definition with prefixed namespace
    primarykey
    data
    text
    <p>I have operators declared for class <code>my_type</code> in namespace <code>my_namespace</code>.</p> <pre><code>namespace my_namespace { class my_type { friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; out, my_type t); } } </code></pre> <p>I'm trying to define these operators in implementation file, but when I write something like that</p> <pre><code>std::ostream&amp; my_namespace::operator &lt;&lt; (std::ostream&amp; out, my_type t) { out &lt;&lt; t; return out; } </code></pre> <p>I get error message</p> <p><code>error: ... operator should have been declared inside 'my_namespace'</code></p> <p>When I change it to</p> <pre><code>namespace my_namespace { std::ostream&amp; operator &lt;&lt; (std::ostream&amp; out, my_type t) { out &lt;&lt; t; return out; } } </code></pre> <p>then it's compiles, but I don't understand the problem. Why does this failed to compile? Is there everything right with that? I would appreciate link to standard as I really don't know what to search.</p> <p><strong>added</strong></p> <p><code>file.h</code></p> <pre><code>#ifndef A_H #define A_H #include &lt;iosfwd&gt; namespace N { class A { friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; out, A&amp; obj); }; } #endif </code></pre> <p><code>file.cpp</code></p> <pre><code>#include "file.h" #include &lt;iostream&gt; std::ostream&amp; N::operator &lt;&lt; (std::ostream&amp; out, N::A&amp; obj) { return out; } int main() {} </code></pre> <p>here is complete example. This works fine on VS2010, but gives above mentioned error on gcc 4.4.5.</p> <p><strong>added</strong></p> <p>hmm...yes, this one works</p> <pre><code>namespace N { class A { friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; out, A&amp; obj); }; std::ostream&amp; operator &lt;&lt; (std::ostream&amp; out, A&amp; obj); } </code></pre> <p>I was always thinking that in terms of visibility declaring friend operator in class is same thing as declaring outside of the class..looks like it's not. Thanks.</p> <p>Thanks much in advance.</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. 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