小编And*_*sca的帖子

ofstream作为c ++中的方法参数

我需要一些帮助.我知道你可以拥有这样的功能

void foo (std::ofstream& dumFile) {}
Run Code Online (Sandbox Code Playgroud)

但我有一个类,我想做同样的事情,编译器给了我很多错误.

我的main.cpp文件如下所示:

#include <iostream>
#include <fstream>
#include "Robot.h"
using namespace std;

ofstream fout("output.txt");

int main() {
    Robot smth;
    smth.Display(fout);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的Robot.h看起来像这样:

#include <fstream>
class Robot{
private:
     int smth;
public:
     void Display(ofstream& fout) {
         fout << "GET ";
     }
};
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试编译这个,我会得到这个错误:

error: ‘ofstream’ has not been declared
 error: invalid operands of types ‘int’ and ‘const char [5]’ to binary ‘operator<<’
Run Code Online (Sandbox Code Playgroud)

任何帮助都非常感谢.

c++ fstream ofstream

0
推荐指数
1
解决办法
311
查看次数

标签 统计

c++ ×1

fstream ×1

ofstream ×1