ostream as parameter for constructor

A

Art Werschulz

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::eek:stream os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::eek:stream 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.
 
P

Piyo

Art said:
Suggestions? Thanks.

Try this instead:
HTH!!

#include <iostream>
#include <string>

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

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

return 0;
}
 
J

John Harrison

Art said:
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::eek:stream os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::eek:stream 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::eek:stream& os_param=std::cout): os(&os_param) {}
void print(std::string s) { (*os) << s; }
private:
std::eek:stream* os;
};

john
 
D

Doug

Art said:
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::eek:stream os_param=std::cout): os(os_param) {}
void print(std::string s) { os << s; }
private:
std::eek:stream 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.

Hiya,

This is a guess, but I doubt you can pass a std::eek:stream by value.
Passing by value will invoke the copy ctor, and a copy ctor for
std::eek:stream 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top