有人给了我以下 C++ 代码片段来试用 - 现在我已经与他们失去联系了(说来话长)。无论如何,它不会编译 - 我得到一个错误
error C2512: 'mstream' : no appropriate default constructor available
任何人都可以解释原因,以及需要什么来修复它。
class mstream : private ostream
{
public:
mstream& operator << (char *value)
{
printf ("[%s]\n", value);
return *this;
}
mstream& operator << (int value)
{
printf ("[%u]\n", value);
return *this;
}
};
mstream g_mcout;
编辑:糟糕,错过了这个......
ostream& mcout ()
{
return g_mcout;
}
ostream& SgDebug ()
{
return g_mcout;
}
仅供引用:这个看起来很奇怪的代码的原因是将 C++ 与 C 程序合并。 printf() 实际上将更改为 my_printf(),后者执行各种自定义操作。
最佳答案
ostream
没有默认构造函数;因此,为 mstream
隐式创建的默认构造函数无效。您需要为 ostream
提供流缓冲区:
class mstream : private ostream
{
public:
mstream() :
ostream(/* whatever you want */)
{}
/* Maybe this is more appropriate:
mstream(std::streambuf* pBuffer) :
ostream(pBuffer)
{}
*/
// ...
};
所以它可以构建。你放在那里的内容取决于你想做什么。
关于c++ - 为什么会出现这个错误? "no appropriate default constructor available",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3997099/