473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

friend operator << overload ambiguity error


Can anyone enligten me why I get the "ambiguous overload" error from the
code below:

friendop.cpp: In function `int main()':
friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,
std::char_traits<char> >& << Thing&' operator
friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_traits<char>]
friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&)
[with
w_char_type = char, w_traits = std::char_traits<char>]
friendop.cpp:27: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_traits<char>]

The code:

#include <iostream>

class Thing;
namespace std {

template<
typename w_char_type,
class w_traits
basic_ostream<w_char_type, w_traits>& operator << (
basic_ostream<w_char_type, w_traits> & o_ostream,
const ::Thing & value
);

}// namespace std

class Thing
{
template<
typename w_char_type,
class w_traits

friend std::basic_ostream<w_char_type, w_traits>& operator << (
std::basic_ostream<w_char_type, w_traits> & o_ostream,
const ::Thing & value
);

};

int main()
{
Thing b;
std::cout << "b = " << b;
}
Jul 22 '05 #1
5 4993

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:-v********************@speakeasy.net...

Can anyone enligten me why I get the "ambiguous overload" error from the
code below:

friendop.cpp: In function `int main()':
friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,
std::char_traits<char> >& << Thing&' operator
friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_traits<char>]
[snip]
The code:

#include <iostream>

class Thing;
namespace std {
This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning standard. As
soon as you start adding names to it, it will behave differently for you
than for the rest of the world, and so it is then by definition not standard
anymore.

Also, in this case it is the source of your problem. You "polluted" std
with your declaration of operator<< for Thing, and then in the class
declaration of Thing, you forgot to qualify the friend declaration of
operator << to be the one you put in std. Thus you implicitly declared a
second operator<<, which caused the ambiguity flagged by your compiler.
class Thing
{
template<
typename w_char_type,
class w_traits
>

The following declares a new operator<< outside of std namespace
friend std::basic_ostream<w_char_type, w_traits>& operator << ( > std::basic_ostream<w_char_type, w_traits> & o_ostream, const ::Thing & value
);

};


You can fix this error simply by taking your operator<< declaration out of
std, where you never should have put it in the first place.

HTH,

Dave Moore

Jul 22 '05 #2
Dave Moore wrote:
....
class Thing;
namespace std {

This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning standard. As
soon as you start adding names to it, it will behave differently for you
than for the rest of the world, and so it is then by definition not standard
anymore.


I had problems ( a while back, I don't remember exactly now) not having
the << operator in the std namespace.

Anyhow, "pollution" is an issue when there is a possibility of
"polluting" causing inadvertent use. In this case operator<< is
overloaded with a parameter type that is specific to the application
which makes it impossible to be confused with another parameter type.
i.e. Overloading for a specific type is non-polluting since it can't
really be mis-applied.

Also, in this case it is the source of your problem. You "polluted" std
with your declaration of operator<< for Thing, and then in the class
declaration of Thing, you forgot to qualify the friend declaration of
operator << to be the one you put in std.
Ah... yep. Missed that one. Thanks.

You can fix this error simply by taking your operator<< declaration out of
std, where you never should have put it in the first place.


I need to go and figure out what the issue was that led me to put it in
there.
....
I now can't reproduce the error I was seeing (a while back). Out of
std:: it goes...
Jul 22 '05 #3
[ ... ]
class Thing;
namespace std {
This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning

standard. As soon as you start adding names to it, it will behave differently for you than for the rest of the world, and so it is then by definition not standard anymore.


This isn't entirely true -- the standard gives explicit permission for
users to add names to namespace std under the right circumstances. I
don't have my copy of the standard here at work to give all the
details, but if memory serves, overloading on a UDF fulfills one of the
major requirements.

--
Later,
Jerry.

Jul 22 '05 #4

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
[ ... ]
class Thing;
namespace std {


This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning

standard. As
soon as you start adding names to it, it will behave differently for

you
than for the rest of the world, and so it is then by definition not

standard
anymore.


This isn't entirely true -- the standard gives explicit permission for
users to add names to namespace std under the right circumstances. I
don't have my copy of the standard here at work to give all the
details, but if memory serves, overloading on a UDF fulfills one of the
major requirements.


You are absolutely right .. I have heard the wisdom espoused in my earlier
"preachy" post so many times that I just regurgitated it without checking.
This is what the Standard has to say about what can and cannot be added to
namespace std (17.4.3.1/1):

"It is undefined for a C++ program to add declarations or definitions to
namespace std or namespaces within namespace std unless otherwise specified.
A program may add template specializations for any standard library template
to namespace std. Such a specialization (complete or partial) of a standard
library template results in undefined behavior unless the declaration
depends on a user-defined name of external linkage and unless the
specialization meets the standard library requirements for the original
template."

The OP's declaration is for a new template function, not a specialization,
so it does not meet the criteria described above. I also checked out the
section on output streams (27.6.2), where basic_ostream is defined, and I
didn't see any further relaxation of the "user cannot inject names into std
namespace" restriction. Thus it seems he is "not allowed" to put his
declaration in std after all.

So, although my general statement was inaccurate, I think the overall
sentiment was on the mark. A relevant question to the OP is also why he
thought he needed to put his operator<< in std anyway .. I have lots of
similar definitions, and they are all in my own namespaces (and very
occasionally in the global namespace).

Hope this clears things up ...

Dave Moore
<gives self "two for preaching" .... *smack* ... *smack* >
Jul 22 '05 #5
[ ... ]
The OP's declaration is for a new template function, not a specialization, so it does not meet the criteria described above. I also checked out the section on output streams (27.6.2), where basic_ostream is defined, and I didn't see any further relaxation of the "user cannot inject names into std namespace" restriction. Thus it seems he is "not allowed" to put his
declaration in std after all.


Right -- I didn't intend to say that the basic idea was wrong, or that
the OP's code was right, merely that when you pointed out his error,
you went on to over-generalize to the point of saying something that
was wrong, and would be misleading if taken on its own.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
6
by: gg | last post by:
If I have the following class - template <class T> class X { public: void dump(ostream & os) { // dump obj_ to os } private: T obj_; };
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
25
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
7
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
6
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor...
2
by: Joe Hesse | last post by:
Hi, I have a template class and I want to define an operator << as a friend function. For each instantiation of the class I want a corresponding instantiation of operator <<. The following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.