473,554 Members | 2,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ost ream<char,
std::char_trait s<char> >& << Thing&' operator
friendop.cpp:27 : candidates are: std::basic_ostr eam<_CharT, _Traits>&
operator<<(std: :basic_ostream< _CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_trait s<char>]
friendop.cpp:14 : std::basic_ostr eam<_CharT, _Traits>&
std::operator<< (std::basic_ost ream<_CharT, _Traits>&, const Thing&)
[with
w_char_type = char, w_traits = std::char_trait s<char>]
friendop.cpp:27 : std::basic_ostr eam<_CharT, _Traits>&
operator<<(std: :basic_ostream< _CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_trait s<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_ostr eam<w_char_type , w_traits>& operator << (
std::basic_ostr eam<w_char_type , w_traits> & o_ostream,
const ::Thing & value
);

};

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

"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:-v************** ******@speakeas y.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_ost ream<char,
std::char_trait s<char> >& << Thing&' operator
friendop.cpp:27 : candidates are: std::basic_ostr eam<_CharT, _Traits>&
operator<<(std: :basic_ostream< _CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_trait s<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_ostr eam<w_char_type , w_traits>& operator << ( > std::basic_ostr eam<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.goo glegroups.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
2081
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
6
1768
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
4660
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++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator...
25
2804
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 contents" ) ; row << new MyTableCell( "cell2 text contents" ) ; table << row ; thanks,
7
1573
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 need use Overloaded + Operator ??? Thanks and sorry my bad English.
4
6739
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 it caught anything, it's very picky... Anyway, the code below works on Dev, and it compiles fine on Borland, but when I run it from borland, there...
9
2370
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 attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator...
6
2075
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 first=second.operator+; the question is the fourth line is all right?
2
1922
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 example fails to compile with g++ version 4.1.2. I would appreciate it if you could help me fix it or point me to a suitable reference. Thank you,
0
7600
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8042
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7889
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6145
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5436
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5155
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3560
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.