错误C2512:'std :: basic_ostream< _Elem,_Traits>' :没有适当的默认构造函数只与Visual Studio可用 [英] error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available with Visual Studio only

查看:681
本文介绍了错误C2512:'std :: basic_ostream< _Elem,_Traits>' :没有适当的默认构造函数只与Visual Studio可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题,因为我有点无奈:这个错误只发生在Visual Studio,GCC编译它没有错误,甚至警告。因为这是一些可移植代码我正在寻找一个解决方案与两个编译器(最好的情况下没有平台依赖 ifdefs )。



> 当构造一个定义为

的MyObject类型的对象时,会发生$ block



<$ p $ <$> MyObject:public Socket,public std :: ostream

code>

code> std :: ostream c $ c>是 std :: basic_ostream< char> 的类型别名。 std :: basic_ostream< char> 的构造函数需要一个指向输出流所关联的流缓冲器的指针。您必须提供一个。



构造函数的签名如下:

  explicit basic_ostream(std :: basic_streambuf< CharT,Traits> * sb); 

您的类构造函数应如下所示:

  class MyObject:public Socket,public std :: ostream 
{
MyObject(/ * ... * /)

std :: ostream(/ *提供一个ptr到这里的流缓冲区/ *)
// ...
{
// ...
}
};

另请参阅以供参考。


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?

解决方案

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.

这篇关于错误C2512:'std :: basic_ostream&lt; _Elem,_Traits&gt;' :没有适当的默认构造函数只与Visual Studio可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆