简体   繁体   中英

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

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

Error C2146 syntax error: missing ';' before identifier '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

Error C2065 'aPolicy': undeclared identifier 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

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 :

#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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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