c++ - 为什么会出现这个错误? "no appropriate default constructor available"

标签 c++

有人给了我以下 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/

相关文章:

c++ - 如何从 Windows LPBITMAP 或 HBITMAP 获取实际的字节数组?

C++循环遍历目录中的文件

c++ - 使用新的外部函数范围声明和初始化变量

c++ - 程序无法编译,使用带有结构对象的数组

c++ - 将 char 转换为算术运算符

c++ - 使用 std::transform 最终 vector 保持为空

c++ - 使用 librsvg 时出现编译错误

c++ - 如何检查 C++ 中的 std::map 插入是否失败?

c++ - 适用于 Mac 应用程序的良好 IMAP 库

c++ - C 与 C++ 数值配方