嗨,我是一个c++
初学者,这是我的作业之一,我有点卡住了。这不是我的全部代码,它只是我需要帮助的一小部分。我想做的是有一个函数专门用于将具有该函数的所有内容导出到text
文件名为 results.txt。因此,当我打开文件时,应该会显示“does this work”这一行,但是当我运行该文件时,我会收到如下错误
“错误 C2065:‘out’:未声明的标识符”
“错误 C2275:'std::ofstream':非法使用此类型作为表达式”
“IntelliSense:不允许类型名称”
“IntelliSense:标识符“out”未定义”
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//prototypes
void output(ofstream& out);
int main()
{
output(ofstream& out);
ifstream in;
in.open("inven.txt");
ofstream out;
out.open("results.txt");
return 0;
}
void output(ofstream& out)
{
out << "does this work?" << endl;
}
现在已经很晚了,我只是对自己做错的事情感到茫然。
首先,这很好:
void output(ofstream& out)
{
out << "does this work?" << endl;
}
然而,这不是:
int main()
{
output(ofstream& out); // what is out?
ifstream in;
in.open("inven.txt");
ofstream out;
out.open("results.txt");
return 0;
}
这是您收到的第一个错误:“错误 C2065:‘out’:未声明的标识符”,因为编译器还不知道 out。
在你想要的第二个片段中call输出具有特定的ostream&
。您不是调用函数,而是给出函数声明,这在此上下文中是不允许的。你必须用给定的来调用它ostream&
:
int main()
{
ifstream in;
in.open("inven.txt");
ofstream out;
out.open("results.txt");
output(out); // note the missing ostream&
return 0;
}
在这种情况下你call output
with out
作为参数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)