在类中定义输出文件流
如何在类中定义输出文件流,以便我不必不断将其传递给函数。基本上我想做的是这样的:
class A {
private:
ofstream otp ;
};
然后在我的构造函数中,我只需有 otp.open("myfile"); ,在其他函数中我有 otp.open("myfile", ios::app); ,但在编译时失败,说:
../thermo.h(18): error: identifier "ofstream" is undefined
ofstream otp ;
我已确保 #include
谢谢!
How can I define an output file stream within a class, so that I don't have to keep passing it around to functions. Basically what I want to do is this:
class A {
private:
ofstream otp ;
};
Then in my constructor, I simply have otp.open("myfile");
and in other functions I have otp.open("myfile", ios::app);
, but it fails during compile time, saying:
../thermo.h(18): error: identifier "ofstream" is undefined
ofstream otp ;
I have made sure to #include <fstream>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用完全限定名称
std::ofstream
。You'll need to use the fully qualified name,
std::ofstream
.您需要在类的声明上方放置一个
using namespace std;
语句,或者将otp
变量声明为std::ofstream
因为它存在于std
命名空间。You need either place a
using namespace std;
statement above your class's declaration or declare theotp
variable asstd::ofstream
because it exists within thestd
namespace.