473,721 Members | 2,103 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 5010

"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
2091
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
6
1776
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
4671
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 #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
25
2849
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
1579
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
6746
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 is no output, no error, it just skips right over the freind ostream call. HELP! I'm new to this...
9
2382
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 ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
6
2083
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
1930
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
8736
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9228
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9146
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5993
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4498
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2592
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.