473,473 Members | 4,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Implementing operator<<

Hi all

I have a class StreamLogger which implements operator << in this way:

template <class TStreamLogger& operator<<(const T& t)
{
<print the stuff using fstream, etc.>

return *this;
}

It works perfectly, using it like:
m_logger << "My ID is " << getID() << "\n";

but for some reason it does not compile this code:
m_logger << std::endl;

It gives the following error:
--------------------------------------------------------------------------------
..\src\Dealer.cpp(70) : error C2678: binary '<<' : no operator found
which takes a left-hand operand of type 'StreamLogger' (or there is no
acceptable conversion)
C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include
\afx.h(1428): could be 'CDumpContext &operator <<(CDumpContext
&,ATL::CTimeSpan)'
....
--------------------------------------------------------------------------------

Could you guys help me understand why it doesn't compile? (because I
dont have a clue...)
Thanks!!!

Jorge
Jan 17 '08 #1
2 2818
On 17 Led, 15:34, soy.h...@gmail.com wrote:
Hi all

I have a class StreamLogger which implements operator << in this way:

template <class TStreamLogger& operator<<(const T& t)
{
<print the stuff using fstream, etc.>

return *this;

}

It works perfectly, using it like:
m_logger << "My ID is " << getID() << "\n";

but for some reason it does not compile this code:
m_logger << std::endl;

It gives the following error:
--------------------------------------------------------------------------------
.\src\Dealer.cpp(70) : error C2678: binary '<<' : no operator found
which takes a left-hand operand of type 'StreamLogger' (or there is no
acceptable conversion)
C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include
\afx.h(1428): could be 'CDumpContext &operator <<(CDumpContext
&,ATL::CTimeSpan)'
...
--------------------------------------------------------------------------------

Could you guys help me understand why it doesn't compile? (because I
dont have a clue...)

Thanks!!!

Jorge

You have to do following steps:

1. Create your own manipulator in std:
namespace std
{
StreamLogger& endl(StreamLogger& s)
{
// Here do what manipulator should do
return s;
}

StreamLogger& hex(StreamLogger& s)
{
// Here do what manipulator should do
return s;
}

// Define all standard manipulators which do not have parameters
// ...
}

2. Overload operator<< to accept function of the same type as
manipulator is:
StreamLogger& operator<<(StreamLogger& s, StreamLogger& (*manip)
(StreamLogger& s))
{
return manip(s);
}

3. Use your manipulator in usual way:
StreamLogger sl;
sl << std::endl;

You should define all standard manipulators for your StreamLogger to
have standard interface. Do not ask me how to support anipulators with
parameters. I know it is (of course) possible, but I do not remember
the solution.
Jan 17 '08 #2
On Jan 17, 3:34 pm, soy.h...@gmail.com wrote:
I have a class StreamLogger which implements operator << in this way:
template <class TStreamLogger& operator<<(const T& t)
{
<print the stuff using fstream, etc.>
return *this;
}
It works perfectly, using it like:
m_logger << "My ID is " << getID() << "\n";
but for some reason it does not compile this code:
m_logger << std::endl;
The problem is that std::endl is also a template. The actual
instantiation of the template will be determined by the type of
the parameter of operator<<, and the type of the parameter of
operator<< will be determined by the type of the argument, i.e.
the type of the instantiated std::endl template.

You'll run into this problem any time you try to output
something that is an uninstantiated template (or an overloaded
function)---in practice, that means the standard manipulators.
The solution is to add a non-template overload to catch
them---template argument deduction fails for your function
template, but the non-template overloads are still considered.
Something like:

inline OutputStreamWrapper const&
operator<<(
OutputStreamWrapper const&
dest,
std::ios_base& (* manip)( std::ios_base& ) )
{
std::ostream* stream = dest.stream() ;
if ( stream != NULL ) {
*stream << manip ;
}
return dest ;
}

inline OutputStreamWrapper const&
operator<<(
OutputStreamWrapper const&
dest,
std::ostream& (* manip)( std::ostream& ) )
{
std::ostream* stream = dest.stream() ;
if ( stream != NULL ) {
*stream << manip ;
}
return dest ;
}

seems to be sufficient. (I note that in my code, I've also got
a non-template overload for char const*. I don't remember why;
whether there is some fundamental problem due to the actual type
of the string literal being char const[], or if it is just a
work-around for a compiler bug somewhere. In general, however:
most of the time you encounter a problem, it will be because
template argument deduction isn't able to handle the case, and
an explicit non-template overload will do the trick.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 18 '08 #3

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

Similar topics

3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
4
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...
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
2
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...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
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
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,...
1
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...
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.