gpt4 book ai didi

c++ - 错误 C2512 : 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available with Visual Studio only

转载 作者:行者123 更新时间:2023-11-30 01:23:08 26 4
gpt4 key购买 nike

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

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

在构造定义为 MyObject 类型的对象时发生

   class MyObject : public Socket, public std::ostream

VS 在这里需要哪些特殊的 ostream 参数?

最佳答案

std::ostreamstd::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(/* provide a ptr to a stream buffer here /*)
// ...
{
// ...
}
};

另见 this供引用。

关于c++ - 错误 C2512 : 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available with Visual Studio only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15361460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com