我需要些帮助。我知道你可以有这样的功能
void foo (std::ofstream& dumFile) {}但是我有一个类,我想做同样的事情,编译器给了我大量的错误。
我的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;
}我的Robot.h看起来是这样的:
#include <fstream>
class Robot{
private:
int smth;
public:
void Display(ofstream& fout) {
fout << "GET ";
}
};现在,如果我试图编译它,我将得到以下错误:
error: ‘ofstream’ has not been declared
error: invalid operands of types ‘int’ and ‘const char [5]’ to binary ‘operator<<’任何帮助都是非常感谢的。
发布于 2018-03-28 09:28:02
您确实必须尊重名称空间:)
class Robot{
private:
int smth;
public:
void Display(std::ofstream& fout) {
fout << "GET ";
}
};您的主文件有using namespace std;,而Robot.h文件没有。(这很好,因为在头文件中使用“使用命名空间”构造是非常危险的想法)
https://stackoverflow.com/questions/49530709
复制相似问题