【发布时间】:2021-04-25 15:53:53
【问题描述】:
我在一个类中使用了几个函数,它们通过函数接口传递一个ostream,进而可以用来输出错误消息。我曾希望能够将所有 ostream 绑定到一个对象,然后在必要时重定向到一个文件。
我的代码的相关部分如下所示:
#include <iostream>
class Example
{
public:
Example(){} //<--Error: "std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits<char>]" (declared at line 390 of "/usr/include/c++/9/ostream") is inaccessible C/C++(330)
void DoSomething()
{
FunctionWithOstream(out);
}
private:
std::ostream out; //in my case, the ostream is currently not needed for the time being.
void FunctionWithOstream(std::ostream& out)
{
out << "Something";
}
};
在构造函数(或程序中的所有构造函数)的第一个大括号中,我收到以下错误消息:
受保护的函数“std::basic_ostream<_chart _traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]”(在“/usr/include/c++/9/ostream”的第 390 行声明)不能通过 “std::basic_ostream
” 指针或 对象C/C++(410)
或者对于我粘贴在这里的简短示例代码:
"std::basic_ostream<_chart _traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]"(在“/usr/include/c++/9/ostream”的第 390 行声明)是不可访问的C/C++(330)
我希望问题足够清楚,并提前感谢您抽出宝贵时间。
问候 蒂尔曼
【问题讨论】:
-
std::ostream不是默认可构造的,您可能需要引用/指针。 -
如果当前不需要,则将其删除,直到需要为止。到那时,你必须做什么会更清楚(即以某种方式构造它,或使用不同的类型)。
-
@Jarod42 那会是什么样子?关于函数接口,我无能为力,因为有些函数使用 ostream,而这些函数又会从接口中包含 ostream 的其他头文件中调用函数。
标签: c++ c++11 iostream ostream