std::ostream class 中的对象是 "not accessible through a pointer or object"
std::ostream object in class is "not accessible through a pointer or object"
我在 class 中使用了几个函数,这些函数通过函数接口传递给一个 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";
}
};
在构造函数(或程序中的所有构造函数)的第一个大括号处,我收到以下错误消息:
protected function "std::basic_ostream<_CharT,
_Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]" (declared at line 390 of "/usr/include/c++/9/ostream") is not accessible through a
"std::basic_ostream<char, std::char_traits>" pointer or
objectC/C++(410)
或者我在此处粘贴的简短示例代码:
"std::basic_ostream<_CharT, _Traits>::basic_ostream() [with
_CharT=char, _Traits=std::char_traits]" (declared at line 390 of "/usr/include/c++/9/ostream") is inaccessibleC/C++(330)
我希望问题足够清楚,提前感谢您的宝贵时间。
问候
蒂尔曼
好的,我找到了解决方案:
#include <iostream>
#include <sstream>
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(outStream);
}
private:
std::ostringstream outStream; //std::ostringstream instead of std::ostream is working fine.
void FunctionWithOstream(std::ostream& out)
{
out << "Something";
}
};
而不是 std::ostream std::ostringstream 可用于接收函数流。
std::ostream
不是默认构造的,您可能需要 reference/pointer 代替:
class Example
{
public:
Example(std::ostream& out = std::cout) : out(out) {}
void DoSomething() { FunctionWithOstream(out); }
private:
std::ostream& out;
void FunctionWithOstream(std::ostream& os) { os << "Something"; }
};
我在 class 中使用了几个函数,这些函数通过函数接口传递给一个 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";
}
};
在构造函数(或程序中的所有构造函数)的第一个大括号处,我收到以下错误消息:
protected function "std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]" (declared at line 390 of "/usr/include/c++/9/ostream") is not accessible through a "std::basic_ostream<char, std::char_traits>" pointer or objectC/C++(410)
或者我在此处粘贴的简短示例代码:
"std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]" (declared at line 390 of "/usr/include/c++/9/ostream") is inaccessibleC/C++(330)
我希望问题足够清楚,提前感谢您的宝贵时间。
问候 蒂尔曼
好的,我找到了解决方案:
#include <iostream>
#include <sstream>
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(outStream);
}
private:
std::ostringstream outStream; //std::ostringstream instead of std::ostream is working fine.
void FunctionWithOstream(std::ostream& out)
{
out << "Something";
}
};
而不是 std::ostream std::ostringstream 可用于接收函数流。
std::ostream
不是默认构造的,您可能需要 reference/pointer 代替:
class Example
{
public:
Example(std::ostream& out = std::cout) : out(out) {}
void DoSomething() { FunctionWithOstream(out); }
private:
std::ostream& out;
void FunctionWithOstream(std::ostream& os) { os << "Something"; }
};