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

ostream as parameter for constructor

Hi.

I would like one of the constructors for a given class to take an
ostream as a parameter.

The following example (foo.cc) doesn't work:

---%<------%<------%<------%<--Cut-Here-%<------%<------%<------%<------
#include <iostream>
#include <string>

class X {
public:
X(std::ostream os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::ostream os;
};

int main()
{
X x(std::cerr);
x.print("hello, world\n");

return 0;
}
---%<------%<------%<------%<--Cut-Here-%<------%<------%<------%<------

I get the following error msg (slightly reformatted to fit the line width):

foo.cc: In copy constructor `std::basic_ios<char,
std::char_traits<char::basic_ios(const std::basic_ios<char,
std::char_traits<char&)':
/usr/include/gcc/darwin/4.0/c++/bits/ios_base.h:736: error:
'std::ios_base::ios_base(const std::ios_base&)' is private
foo.cc:6: error: within this context

(Line 6 of foo.cc is the definition of the X constructor.)

Suggestions? Thanks.

--
Art Werschulz (agw STRUDEL comcast.net)
207 Stoughton Ave Cranford NJ 07016
(908) 272-1146
Mar 9 '07 #1
4 4515
Art Werschulz wrote:
>
Suggestions? Thanks.
Try this instead:
HTH!!

#include <iostream>
#include <string>

class X {
public:
X(std::ostream &os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::ostream &os;
};

int main()
{
X x(std::cerr);
x.print("hello, world\n");

return 0;
}
Mar 9 '07 #2
Art Werschulz wrote:
Hi.

I would like one of the constructors for a given class to take an
ostream as a parameter.

The following example (foo.cc) doesn't work:

---%<------%<------%<------%<--Cut-Here-%<------%<------%<------%<------
#include <iostream>
#include <string>

class X {
public:
X(std::ostream os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::ostream os;
};

int main()
{
X x(std::cerr);
x.print("hello, world\n");

return 0;
}
---%<------%<------%<------%<--Cut-Here-%<------%<------%<------%<------

I get the following error msg (slightly reformatted to fit the line width):

foo.cc: In copy constructor `std::basic_ios<char,
std::char_traits<char::basic_ios(const std::basic_ios<char,
std::char_traits<char&)':
/usr/include/gcc/darwin/4.0/c++/bits/ios_base.h:736: error:
'std::ios_base::ios_base(const std::ios_base&)' is private
foo.cc:6: error: within this context

(Line 6 of foo.cc is the definition of the X constructor.)

Suggestions? Thanks.
streams are not copyable. You must pass a reference or a pointer to a
stream, for instance

class X {
public:
X(std::ostream& os_param=std::cout): os(&os_param) {}
void print(std::string s) { (*os) << s; }
private:
std::ostream* os;
};

john
Mar 9 '07 #3

Art Werschulz wrote:
Hi.

I would like one of the constructors for a given class to take an
ostream as a parameter.

The following example (foo.cc) doesn't work:

---%<------%<------%<------%<--Cut-Here-%<------%<------%<------%<------
#include <iostream>
#include <string>

class X {
public:
X(std::ostream os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::ostream os;
};

int main()
{
X x(std::cerr);
x.print("hello, world\n");

return 0;
}
---%<------%<------%<------%<--Cut-Here-%<------%<------%<------%<------

I get the following error msg (slightly reformatted to fit the line width):

foo.cc: In copy constructor `std::basic_ios<char,
std::char_traits<char::basic_ios(const std::basic_ios<char,
std::char_traits<char&)':
/usr/include/gcc/darwin/4.0/c++/bits/ios_base.h:736: error:
'std::ios_base::ios_base(const std::ios_base&)' is private
foo.cc:6: error: within this context

(Line 6 of foo.cc is the definition of the X constructor.)

Suggestions? Thanks.

--
Art Werschulz (agw STRUDEL comcast.net)
207 Stoughton Ave Cranford NJ 07016
(908) 272-1146
Hiya,

This is a guess, but I doubt you can pass a std::ostream by value.
Passing by value will invoke the copy ctor, and a copy ctor for
std::ostream doesn't make much sense to me.

Try passing the ostream by pointer?

I'm probably wrong, but don't have a compiler or any source to check.
Good luck.

Doug

Mar 9 '07 #4
Hi.

Thanks for your suggestions.

--
Art Werschulz (agw STRUDEL comcast.net)
207 Stoughton Ave Cranford NJ 07016
(908) 272-1146
Mar 9 '07 #5

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

Similar topics

8
by: Boris | last post by:
Is it possible to manipulate the std::ostream to prepend a string when performing output, e.g. // manipute std::cout to prepend "prefix " std::cout << "hallo" << std::endl; // results in...
6
by: pembed2003 | last post by:
Hi all, Given something like: std::ofstream out_file("path"); how do I extract the file descriptor from out_file? Is it possible? What I want is to extract the file descriptor and then pass...
7
by: Robert Sherry | last post by:
It is my understanding that the standard variable cout is of type ostream. Please consider the following C++ program. #include <iostream> using namespace std; void func1( ostream os1 ); ...
1
by: hitech_guy | last post by:
Hi, I am creating my own class derving from ostream. I am facing problem in compiling with GNU G++ compiler under linux class IWCoreEXPORT IWAFTraceLine : public ostream { public:...
13
by: Angel Tsankov | last post by:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
5
by: Julian | last post by:
I have this piece of code that is working fine in VC6... but when i tried to compile the same code in VC++.NET, I am getting this error : error C2512: 'std::basic_ostream<_Elem,_Traits>' : no...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
5
by: lars | last post by:
Hi, I have C (printf) style debug functions static void debugPrint(Silver::Strategy& s, unsigned level, const char *format, ...) { if (s.verbosityLevel() >= level ) { va_list args ;...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
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...
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
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,...
0
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...
0
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...

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.