By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

ostream as parameter for constructor

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.