Reputation: 7
I use several functions in a class, which are passed an ostream via the function interface, which in turn can be used to output error messages. I had hoped to be able to bind all ostreams to a single object that I then redirect to a file if necessary.
The relevant parts of my code look something like this:
#include <iostream>
class Example
{
public:
Example(){} //<--Error: "std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits<char>]" (declared at line 390 of "/usr/include/c++/9/ostream") is inaccessible C/C++(330)
void DoSomething()
{
FunctionWithOstream(out);
}
private:
std::ostream out; //in my case, the ostream is currently not needed for the time being.
void FunctionWithOstream(std::ostream& out)
{
out << "Something";
}
};
At the first curly bracket of the constructor (or all constructors in the program) I get the following error message:
protected function "std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]" (declared at line 390 of "/usr/include/c++/9/ostream") is not accessible through a "std::basic_ostream<char, std::char_traits>" pointer or objectC/C++(410)
or for the short excample code i pasted here:
"std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]" (declared at line 390 of "/usr/include/c++/9/ostream") is inaccessibleC/C++(330)
I hope the question is clear enough and thank you in advance for your time.
Greetings Tillman
Upvotes: 0
Views: 421
Reputation: 217065
std::ostream
is not default constructible, you probably want reference/pointer instead:
class Example
{
public:
Example(std::ostream& out = std::cout) : out(out) {}
void DoSomething() { FunctionWithOstream(out); }
private:
std::ostream& out;
void FunctionWithOstream(std::ostream& os) { os << "Something"; }
};
Upvotes: 2
Reputation: 7
Ok, I have found a solution:
#include <iostream>
#include <sstream>
class Example
{
public:
Example(){} //<--Error: "std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits<char>]" (declared at line 390 of "/usr/include/c++/9/ostream") is inaccessible C/C++(330)
void DoSomething()
{
FunctionWithOstream(outStream);
}
private:
std::ostringstream outStream; //std::ostringstream instead of std::ostream is working fine.
void FunctionWithOstream(std::ostream& out)
{
out << "Something";
}
};
instead of std::ostream std::ostringstream can be used to receive the functions streams.
Upvotes: 0