#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 &#39; outFile&#39;:未声明的标识符 Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 39
错误 C2228 左边的&#39; .open&#39;必须有class / struct / union Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 39
错误 C2065 &#39;政策&#39;:未声明的标识符 Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 40
错误 C2146 语法错误:缺少&#39;;&#39;在标识符&#39; aPolicy&#39;之前 Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 40
错误 C2065 &#39; aPolicy&#39;:未声明的标识符 Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 40
错误 C2065 &#39; aPolicy&#39;:未声明的标识符 Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 44
错误 C2065 &#39; aPolicy&#39;:未声明的标识符 Project6 c:\ users \ preston freeman \ source \ repos \ jave.cpp 45
如何解决这些错误? 谢谢你的时间?
答案 0 :(得分:0)
代码中有很多拼写错误,但主要问题是没有已知的从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;
}