问题
I'm asking this question because I'm a bit helpless: this error occurs ONLY with Visual Studio, GCC compiles it without errors or even warnings. Since this is some portable code I'm looking for a solution that works with both compilers (and in best case with no platform-dependent ifdefs
).
error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available
happens while constructing an object of type MyObject that is defined as
class MyObject : public Socket, public std::ostream
What special ostream
parameters are expected by VS here?
回答1:
std::ostream
is a type alias for std::basic_ostream<char>
. The constructor of std::basic_ostream<char>
expects a pointer to a stream buffer to which the output stream is associated. You must provide one.
The signature of the constructor is the following one:
explicit basic_ostream( std::basic_streambuf<CharT, Traits>* sb );
Your class constructor should look something like this:
class MyObject : public Socket, public std::ostream
{
MyObject(/* ... */)
:
std::ostream(/* provide a ptr to a stream buffer here /*)
// ...
{
// ...
}
};
Also see this for a reference.
来源:https://stackoverflow.com/questions/15361460/error-c2512-stdbasic-ostream-elem-traits-no-appropriate-default-constr