简体   繁体   English

使用fstream的程序将不合规

[英]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;
    }

} This program should accept values from the keyboard and print them into a file. }此程序应从键盘接受值并将它们打印到文件中。 However, it is giving a bunch of syntax errors. 但是,它给出了许多语法错误。

Severity Code Description Project File Line Suppression State Error C2065 'outFile': undeclared identifier Project6 c:\\users\\preston freeman\\source\\repos\\jave.cpp 39 严重性代码说明项目文件行抑制状态错误C2065'outFile':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 39

Error C2228 left of '.open' must have class/struct/union 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

Error C2065 'Policy': undeclared identifier Project6 c:\\users\\preston freeman\\source\\repos\\jave.cpp 40 错误C2065“政策”:未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 40

Error C2146 syntax error: missing ';' 错误C2146语法错误:缺少';' before identifier 'aPolicy' Project6 c:\\users\\preston freeman\\source\\repos\\jave.cpp 40 标识符'aPolicy'Project6之前c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 40

Error C2065 'aPolicy': undeclared identifier Project6 c:\\users\\preston freeman\\source\\repos\\jave.cpp 40 错误C2065'aPolicy':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 40

Error C2065 'aPolicy': undeclared identifier Project6 c:\\users\\preston freeman\\source\\repos\\jave.cpp 44 错误C2065'aPolicy':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 44

Error C2065 'aPolicy': undeclared identifier Project6 c:\\users\\preston freeman\\source\\repos\\jave.cpp 45 错误C2065'aPolicy':未声明的标识符Project6 c:\\ users \\ preston freeman \\ source \\ repos \\ jave.cpp 45

How do I fix these errors? 如何解决这些错误? thank you for your time? 感谢您的时间?

There are many typos in the code but main issue there is no known conversion from fstream to ofstream so here is correct version of the code : 代码中有很多错别字,但是主要的问题是从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM