473,385 Members | 1,846 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,385 software developers and data experts.

No match for operator<< when using std::endl

When compiling the below I get the following error:
no match for 'operator<<' in 'log << std::endl' main.cpp

Why can't I use std::endl in my class?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class Logger {
  4.         public:
  5.                 template <typename T>
  6.                 Logger& operator << (const T &t)
  7.                 {
  8.                     // do sth with t
  9.                     std::cout << t;
  10.                     return *this;
  11.                 }
  12. };
  13.  
  14. int main()
  15. {
  16. Logger log;
  17.  
  18. log << "Hello " << 10 << 20;  // ok
  19. log << std::endl; // fault here
  20.  
  21. }
  22.  
Mar 9 '10 #1
5 5690
weaknessforcats
9,208 Expert Mod 8TB
The operator<< cannot be a member function. This is because the first argument of the << function must be the object to the left of the operator and the second argument is the object to the right of the operator. LIke this:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, Logger& obj);
As a member function, the compiler will insert the this pointer of your Logger object as the first argument. That would be a pointer to a Logger object. std::endl requires an ostream& and not a Logger*.

The solution is to make your operator<< a friend function. It should have the prototype shown above.
Mar 9 '10 #2
Thanks for quick reply weaknessforcats,

but declaring the function this way:
friend ostream& operator<<(ostream& os, Logger& obj)
how can I pass parameters to the Logger class?
Mar 9 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
You have Logger object right?

Expand|Select|Wrap|Line Numbers
  1. Logger obj( the parameters);
  2.  
  3. obj.SetFormat(parameters for formatting the log);
  4.  
  5. //etc...
  6.  
  7. cout << obj << endl;

The code above is a call to

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream&, Logger&);
followed by a call to

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream&, ostream& (*)(ostream&));
The second call is for the endl. The second function is provided by the C++ Library.

You could also:

Expand|Select|Wrap|Line Numbers
  1. cout << Logger(the parameters) << endl;

That would create a Logger object which would be used an then deleted when the satement was complete.
Mar 10 '10 #4
Banfa
9,065 Expert Mod 8TB
weaknessforcats my reading of what JerryBrit is trying to do (and I may be wrong) is that they are not trying to output the data in Logger but that the Logger class is an intercept to say log the output of a program as it is running and although it currently outputs to cout it might in future be altered to output to a file or to both cout and a file.

JerryBrit if my interpretation is wrong you should say because in that case what weaknessforcats is almost certainly right. However if I am right any data in the Logger class is irrelivent and is not being output anyway, Logger needs to receive data in the same way that cout normally does not be capable of being passed to cout.

I think, but listen to weaknessforcats because he really is the expert, that in that case what you would want is your own logging version of endl defined something like

Logger& loggerendl ( Logger& os );

to output an end of line to the log output. Or you would need to define a member function of Logger something like

Logger& Logger::operator<<(ostream& ( *pf )(ostream&));

to accept stream manipulators like endl.
Mar 10 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
You might try:

Expand|Select|Wrap|Line Numbers
  1. class Logger { 
  2. private:
  3.     ofstream theLog;
  4.            public: 
  5.             Logger();
  6.                 template <typename T> 
  7.                 Logger& operator << (const T &t) 
  8.                 { 
  9.                     // do sth with t 
  10.                     this->theLog << t; 
  11.                     return *this; 
  12.                 } 
  13.  
  14. }; 
  15. Logger::Logger(): theLog("TheLogFile.txt")
  16. {
  17.  
  18. }
  19.  
  20. int main() 
  21. Logger log; 
  22.  
  23. log << "Hello " << 10 << 20;  // ok 
  24. log << std::endl; // fault here 
Here I added an ofstream object to your Logger. This the log file. Since it is an ofstream and since ofstream is a kind-of ostream, then a manipulator like std::endl should work with an ofstream object.

Then all I did was add a Logger constructor to get the file name and change your Logger::operator<< to use the ofstream member variable rather than cout.
Mar 12 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
5
by: Eric Lilja | last post by:
Ok, this code doesn't compile: #include <iostream> #include <ostream> /* Just for you, Mike :-) */ template<typename T> class Couple { public: Couple(const T& ax, const T& ay) : x(ax), y(ay)...
4
by: jalkadir | last post by:
This program does compile, but the linker says: main.o(.text+0x1a4):main.cpp: undefined reference to `jme::operator<<(std::ostream&, jme::Name const&)' here is the program's snips. ...
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...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
6
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
1
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
2
by: soy.hohe | last post by:
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; }
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.