473,385 Members | 1,838 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.

function to format message & output to cerr/cout/ostream

Hello,

Please bear with me, I am trying to learn C++. I am implementing some
error/debug functions which format a message and output it to a C++ stream.
I would like to design it using one low level function which accepts a
"generic C++ stream" as an argument (cerr, cout, ofstream, etc). I have
tried to make the parameter a const ostream&, but when I plug in cerr or
cout I get a compiler error. What am I doing wrong?

// The low level function just formats some message and outputs it to
"stream".
void _output(const ostream& stream, const char* reason, int error)
{
ostringstream message;
// ... do some formatting
if (stream == cerr)
cerr << message << endl;
else if (stream == cout)
cout << message << endl;
}

// the high level functions just direct the output to a different stream
void outputerr(const char* reason, int error)
{
// do something special for errors
_output(cerr, reason, error);
}

void outputcmt(const char* reason, int error)
{
// do something special for comments
_output(cout, reason, error);
}

void outputwrn(const char* reason, int error)
{
// do something special for warnings
_output(cout, reason, error);
}

The compiler error is:

binary '<<': no operator found which takes a left-hand operand of type
'const std::ostream' (or there is no acceptable conversion)

on the line of code:

"stream << message.str().c_str() << endl" // compiler error is in _output()
Jul 19 '05 #1
2 7733
Trevor wrote:
Hello,

Please bear with me, I am trying to learn C++. I am implementing
some
error/debug functions which format a message and output it to a C++
stream. I would like to design it using one low level function which
accepts a
"generic C++ stream" as an argument (cerr, cout, ofstream, etc). I
have tried to make the parameter a const ostream&, but when I plug in
cerr or
cout I get a compiler error. What am I doing wrong?

// The low level function just formats some message and outputs it to
"stream".
void _output(const ostream& stream, const char* reason, int error) ^^^^^
Your stream is const, so you cannot write to it.

{
ostringstream message;
// ... do some formatting
if (stream == cerr)
cerr << message << endl;
else if (stream == cout)
cout << message << endl;
}

// the high level functions just direct the output to a different
stream void outputerr(const char* reason, int error)
{
// do something special for errors
_output(cerr, reason, error);
}

void outputcmt(const char* reason, int error)
{
// do something special for comments
_output(cout, reason, error);
}

void outputwrn(const char* reason, int error)
{
// do something special for warnings
_output(cout, reason, error);
}

The compiler error is:

binary '<<': no operator found which takes a left-hand operand of type
'const std::ostream' (or there is no acceptable conversion)
Right. The operator takes an std::ostream&, not a const std::ostream&.

on the line of code:

"stream << message.str().c_str() << endl" // compiler error is in
_output()

Jul 19 '05 #2
Hi Trevor

Your code is fine, just remove the "const" before the ostream& in
_output() (there's no need for the stream to be const, and I'm not
sure of the internal workings of the << operator, so it might be
required to not be const... ):

=================================================

#include <iostream>
#include <sstream>
using namespace std;

// The low level function just formats some message and outputs it to
// "stream".
void _output(ostream& stream, const char* reason, int error)
{
ostringstream message;
// might need a big if/else if/else if to handle formatting for
each stream
message << "Here's generic formatting : " << reason << " ... :-)
....";

// this code is just the same as that which follows
// ... do some formatting
// if (stream == cerr)
// cerr << message.str() << endl; // did you forget the str()
here?
// else if (stream == cout)
// cout << message.str() << endl; // ditto?

// assuming you just meant this?
stream << message.str() << endl; // .c_str() not required

}
// the high level functions just direct the output to a different
stream
// functions are as you showed them
int main() {
outputerr("error here", 2);
outputcmt("comment", 3);

return 0;
}

================================================== ======

Just a few quick notes, though:

1. Try to stay away from starting variable and function names with an
underscore, I don't think this is good style

2. If you're using the outputXXX (eg outputerr, outputcmt) functions
in your code, forwarding all of those function calls to another
routine (_output(), in this case), and then doing all the actual work
in the called routine /might/ not be a good choice, especially if you
have extensive checks and branches within the worker function (e.g.,
if (stream == std::cout) {} else if (stream == std::cerr) {} ... etc
....) Why not just do all the formatting etc in the original function
(outputerr, outputcmt ...)? Mind you, I don't know your design
choices, so you may have good reasons for this.

Good luck! Jeff
Jul 19 '05 #3

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

Similar topics

4
by: Patrick | last post by:
I want to achieve the following behaviour but I am having trouble getting there...bare with me im knew to c++ , so its probably rather trivial! To have a class ClassA, and composed within this...
8
by: Johno | last post by:
I have written the two associated base classes below (Digital_camera and Review) to manage digital camera and review objects. They are base classes for which other derived classes can be written to...
4
by: rossum | last post by:
I have been looking at exceptions as I need to get better at using them. I came across an interesting effect, demonstrated below. When I ctrl-Z the input to throw an ios_base::failure, the...
3
by: Eric Lilja | last post by:
Hi, I need a function that expects a const std::string& containing the visual representation of a number in binary format. It should then output this number in groups of four bits separated by a...
5
by: Tim Clacy | last post by:
When exiting function scope, which occurs first: a) destruction of local objects b) copy of value for return From disassembly of a debug target, it looks like the return value is copied before...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
15
by: Marcus Kwok | last post by:
How do you save the formatting options for a stream? In the program below, notice that when I define my operator<<() for the custom type, it permanently changes the output format of the stream: ...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
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,...

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.