473,883 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error: ambiguous overload for 'operator <<' in...

Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.

The code below compiles fine under vc++, but I'm having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elE nd}'
*/
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )
{
typename C::const_iterat or start( cpi.begin() ); typename
C::const_iterat or end( --(cpi.end() )); typename C::const_iterat or iter;
op << "{";
for (iter=start;ite r!=end; ++iter){
op<<*iter<<",";
}
op<<*iter<<"}";
return op;
};

The offending lines are the
op<<"{";
and
op<<"}";

apparently it doesn't know what to do with a simple string, but it can
handle the template argument...

So am I on topic here, and if so can someone please give me a little
insight? I appreciate any assistance that can be given. Searched the web
but was unable to find anything that helped (I also posted to a g++ group).

Glen

Oct 26 '06 #1
7 14941
glen <st***@ife.ee.e thz.chwrote:
Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.

The code below compiles fine under vc++, but I'm having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elE nd}'
*/
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )
{
typename C::const_iterat or start( cpi.begin() ); typename
C::const_iterat or end( --(cpi.end() )); typename C::const_iterat or iter;
op << "{";
for (iter=start;ite r!=end; ++iter){
op<<*iter<<",";
}
op<<*iter<<"}";
return op;
};

The offending lines are the
op<<"{";
and
op<<"}";

apparently it doesn't know what to do with a simple string, but it can
handle the template argument...

So am I on topic here, and if so can someone please give me a little
insight? I appreciate any assistance that can be given. Searched the web
but was unable to find anything that helped (I also posted to a g++ group).
AFAIK operator<< for a const char* should be available. Since it's only
a single character, maybe you could try

op << '{';

instead.

Another is to double-check that you have all the necessary #includes.
Maybe the Standard Library of VC++ implicitly includes something that
gcc does not. Also, make sure you are using g++ instead of gcc.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Oct 26 '06 #2
On Oct 26, 5:48 pm, glen <s...@ife.ee.et hz.chwrote:
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )
{
typename C::const_iterat or start( cpi.begin() ); typename
C::const_iterat or end( --(cpi.end() )); typename C::const_iterat or iter;
op << "{";
for (iter=start;ite r!=end; ++iter){
op<<*iter<<",";
}
op<<*iter<<"}";
return op;
};

The offending lines are the
op<<"{";
and
op<<"}";
I'm not terribly sure about some of the subtleties here, but I think it
has something to do with the generality of your overload. Maybe "{"
can be implictly cast to a const std::string &, which fits your
template also, but already has its own overloaded operator<< - so which
overload to use is ambiguous.

-steve

Oct 26 '06 #3

glen wrote:
Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.

The code below compiles fine under vc++, but I'm having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elE nd}'
*/
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )
type C is not a container, its just a const type (an element). If you
meant to pass a std::vector by reference:

template< typename C >
std::ostream& operator<< (std::ostream& os, const std::vector< C >& r_v
)
{
... // iterate through the container's elements
}
{
typename C::const_iterat or start( cpi.begin() ); typename
C::const_iterat or end( --(cpi.end() )); typename C::const_iterat or iter;
op << "{";
for (iter=start;ite r!=end; ++iter){
op<<*iter<<",";
}
op<<*iter<<"}";
return op;
};

The offending lines are the
op<<"{";
and
op<<"}";
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>

template< typename T >
std::ostream&
operator<<(std: :ostream& os, const std::vector< T >& r_vc )
{
typedef typename std::vector< T >::const_iterat or VIter;
VIter viter = r_vc.begin();
while (viter != r_vc.end())
{
os << "{ " << *viter++;
os << " }\n";
}
return os;
}

int main()
{
std::vector< std::string vs;
vs.push_back("s tring_0");
vs.push_back("s tring_1");
vs.push_back("s tring_2");

std::vector< double vd;
vd.push_back( 0.0 );
vd.push_back( 1.1 );
vd.push_back( 2.2 );

std::vector< char vc;
vc.push_back('a ');
vc.push_back('b ');
vc.push_back('c ');

std::cout << vs << vd << vc;

return 0;
}

Oct 26 '06 #4
Salt_Peter wrote:
glen wrote:
>Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.

The code below compiles fine under vc++, but I'm having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elE nd}'
*/
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )

type C is not a container, its just a const type (an element).
If you call this inserter with a vector then C is a vector.
while (viter != r_vc.end())
{
os << "{ " << *viter++;
os << " }\n";
}
The formatting that this code does is rather different from what the
orignal code does. It precedes every element with a {, while the
original code put one { at the beginning and separated each pair of
elements with a comma. It skips the tricky part.<g>

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 26 '06 #5
glen wrote:
>
So am I on topic here, and if so can someone please give me a little
insight? I appreciate any assistance that can be given. Searched the web
but was unable to find anything that helped (I also posted to a g++ group).
Marcus's second answer is probably right: the code which you didn't show
doesn't have all of the necessary includes. I'd guess that it doesn't
pull in <ostream>.

Also, the code can be more general if it doesn't try so hard to avoid an
extra test. Instead of looping up to --cpi.end(), which won't compile
for an input iterator or a forward iterator and might not work with an
empty container, just write out the first element, then for each
subsequent element write a comma and the element. Like this:

op << "{";
if (iter != cpi.end())
op << *iter++;
while (iter != cpi.end())
op << "," << *iter++;
op << "}";

I'd use single chars in all three places instead of those quoted
strings, because it's faster, but that doesn't affect the validity of
the code. If you're going to do that, though, wait until you've figured
out why the version with the quoted strings doesn't compile.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 26 '06 #6

Pete Becker wrote:
Salt_Peter wrote:
glen wrote:
Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.

The code below compiles fine under vc++, but I'm having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elE nd}'
*/
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )
type C is not a container, its just a const type (an element).

If you call this inserter with a vector then C is a vector.
Thats fundamentally wrong.
The compiler will attempt to regenerated a new version of op<< every
time that
cout << whatever;
or
os << whatever;
or
os << " ";
is found in the underlying code.
error: ambiguous overload for 'operator<<' is your result.
>
while (viter != r_vc.end())
{
os << "{ " << *viter++;
os << " }\n";
}

The formatting that this code does is rather different from what the
orignal code does. It precedes every element with a {, while the
original code put one { at the beginning and separated each pair of
elements with a comma. It skips the tricky part.<g>
Granted, the coder can adapt the output to whatever he needs.
>
--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 26 '06 #7
Salt_Peter wrote:
Pete Becker wrote:
>Salt_Peter wrote:
>>glen wrote:
Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.

The code below compiles fine under vc++, but I'm having trouble
using gcc. It's just a templated container output.
/**
outputs elements of a vector in the form '{el1,el2...elE nd}'
*/
template<typena me C>
std::ostream& operator<< (std::ostream& op, const C& cpi )
type C is not a container, its just a const type (an element).
If you call this inserter with a vector then C is a vector.

Thats fundamentally wrong.
No, it's absolutely correct.
The compiler will attempt to regenerated a new version of op<< every
time that
cout << whatever;
or
os << whatever;
or
os << " ";
is found in the underlying code.
error: ambiguous overload for 'operator<<' is your result.
For overloading, template specializations are ordered, and the most
specific one wins.
>
>> while (viter != r_vc.end())
{
os << "{ " << *viter++;
os << " }\n";
}
The formatting that this code does is rather different from what the
orignal code does. It precedes every element with a {, while the
original code put one { at the beginning and separated each pair of
elements with a comma. It skips the tricky part.<g>

Granted, the coder can adapt the output to whatever he needs.
Well, sure. But this doesn't address the problem he asked about.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 26 '06 #8

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

Similar topics

7
4348
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
4
2070
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the following will be printed A 3 2
25
2873
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,
2
2188
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
11
3573
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class T> DataDest& operator<<(DataDest& d, const T& t) { t.writeTo(d);
5
3445
by: David | last post by:
Hi all, I am trying to overload the < operator, but get warning class Windowinfo { protected: HWND wndhandle; //the window handle int wndId; //the window Id
0
9944
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9796
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,...
1
10860
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
10420
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
9583
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5804
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
6002
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3239
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.