If you define a operator<<(ostream&,thing) for an object X in one namespace, then in another namespace, define the same sort of function for a different object Y, it hides the original function so you can't print objects of type X in the namespace associated with object Y. Fairly obvious workaround: don't define operator<<'s in namespaces - make sure they always go in the global namespace. Bjarne wouldn't be pleased, though. I'm not 100% certain this is a bug and not a "feature" of C++ - it just looks suspicious. I haven't been able to find this in the list of known bugs, but it doesn't mean it's not there. Thanks for your time, Steev Release: 3.1 (sparc-sun-solaris2.8) Environment: Solaris 8 How-To-Repeat: Compile bug2.cpp with "g++ -c bug2.cpp". It'll fail in the way indicated in file "testout". If you place the definition of the operator<< on Y in the global namespace, g++ can suddenly find the definition of operator<< on X.
Fix: For workaround, see above.
State-Changed-From-To: open->feedback State-Changed-Why: Hi. For some reason, both 'bug2.cpp' and 'testout' got lost. Could you possibly provide them again? Thanks, Paolo.
State-Changed-From-To: feedback->open State-Changed-Why: Attachments provided.
Responsible-Changed-From-To: unassigned->paolo Responsible-Changed-Why: .
State-Changed-From-To: open->closed State-Changed-Why: Philip is right (also double checked with two other respected compilers).
From: Philip Martin <philip@codematters.co.uk> To: steev@paradigmds.com, gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org Cc: Subject: Re: c++/7049 Overloading "cout <<" in one namespace obscures similar functions in a different namespace Date: 18 Jun 2002 12:30:14 +0100 This is not a bug, that's how name lookup works. The compiler will lookup operator<< in the anonymous namespace where the statement "cout << x" occurs, the std:: namespace where ostream is defined, and the ns:: namespace where class X is defined. Since the compiler will find an operator<< by doing this it won't look anywhere else. In particular operator<< in the global namespace will not be considered. Put operator<< for X where it belongs, in the ns:: namespace that defines class X. -- Philip
*** Bug 26037 has been marked as a duplicate of this bug. ***
Reopen to adjust to...
... invalid.
*** Bug 21924 has been marked as a duplicate of this bug. ***