0
votes
#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?

1
Pop quiz: spot the difference: outfile and outFile; also Policy and InsurancePolicy.Sam Varshavchik
When you get errors like this, the first thing to do is read the error messages. #1: Start at the top, read the first one, make the correction (you have to actually read your code to do so), and then try again. #2. If you get errors, go back to #1. Keep doing that until you get no more errors.Ken White
friend fstream& operator<<(fstream&, InsurancePolicy); -- This should be: friend fstream& operator<<(fstream&, const InsurancePolicy&);. You should not be passing objects by value to an output streaming function.PaulMcKenzie

1 Answers

0
votes

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;

}