[英]Program using fstream will not complile
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class InsurancePolicy
{
friend fstream& operator<<(fstream&, InsurancePolicy);
friend istream& operator>>(istream&, InsurancePolicy&);
private:
int policyNum;
string lastName;
int value;
int premium;
};
fstream& operator<<(fstream& out, InsurancePolicy pol)
{
out << pol.policyNum << " " << pol.lastName << " " << pol.value << " " << pol.premium << endl;
return out;
}
istream& operator>>(istream& in, InsurancePolicy& pol)
{
in >> pol.policyNum >> pol.lastName >> pol.value >> pol.premium;
return in;
}
int main()
{
ofstream outfile;
outFile.open("Policy.txt");
Policy aPolicy[10];
for (int count = 0; count < 10; ++count)
{
printf("Enter the policy number, the holder's last name, the value, and the premium.");
cin >> aPolicy[count];
outfile << aPolicy[count] << endl;
}
}此程序应从键盘接受值并将它们打印到文件中。 但是,它给出了许多语法错误。
严重性代码说明项目文件行抑制状态错误C2065'outFile':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 39
错误'.open'左边的错误C2228必须具有class / struct / union Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 39
错误C2065“政策”:未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 40
错误C2146语法错误:缺少';' 标识符'aPolicy'Project6之前c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 40
错误C2065'aPolicy':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 40
错误C2065'aPolicy':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 44
错误C2065'aPolicy':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 45
如何解决这些错误? 感谢您的时间?
代码中有很多错别字,但是主要的问题是从fstream到ofstream没有已知的转换,因此这是代码的正确版本:
#include <iostream>
#include <fstream>
using namespace std;
class InsurancePolicy
{
friend ofstream& operator<<(ofstream&, InsurancePolicy);
friend istream& operator>>(istream&, InsurancePolicy&);
private:
int policyNum;
string lastName;
int value;
int premium;
};
ofstream& operator<<(ofstream& out, InsurancePolicy pol)
{
out << pol.policyNum << " " << pol.lastName << " " << pol.value << " " << pol.premium << endl;
return out;
}
istream& operator>>(istream& in, InsurancePolicy& pol)
{
in >> pol.policyNum >> pol.lastName >> pol.value >> pol.premium;
return in;
}
int main() {
ofstream outFile;
outFile.open("Policy.txt");
InsurancePolicy aPolicy[10];
for (int count = 0; count < 10; ++count)
{
printf("Enter the policy number, the holder's last name, the value, and the premium.");
cin >> aPolicy[count];
outFile << aPolicy[count]<<std::endl;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.