error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available with Visual Studio only

守給你的承諾、 提交于 2019-12-02 12:59:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!