For some reason the ofstream out with the iterator at the bottom is denying access to itself for some reason, something having to do with the pointer? Honestly, me and my friends have been trying to figure out what is wrong here for a long time. Even a veteran who has been coding for years couldn't help me. Any help is appreciated! here is the error:
Error 6 error C2248: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>'
as well as intellisense
7 IntelliSense: "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &_Right) [with _Elem=char, _Traits=std::char_traits]" (declared at line 1034 of "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\fstream") is inaccessible
/*
estimate.cc -- Program to estimate the cost of attendance for students -- Part 1
This part will read the records, place them in separate vectors, sort them,
and then write them out to separate files.
Authors: Larry Morell,
Aaron Wilson
Strategy:
Create a class for a general student called Student
Divide students into three categories: Resident, Commuter, Onliner.
Set up a class for each one of these categories to inherit from Student.
The input file is called students.dat. Its format is as follows.
HourlyRate Fees CentsPerMile -- first line
The remaining lines are one of three formats, one line for each student
R FirstName LastName GPA HoursRegistered Major MealPlan Housing -- for resident student
C FirstName LastName GPA HoursRegistered Major Miles MealPlan -- for commuter student
O FirstName LastName GPA HoursRegistered Major ISP_Cost -- for onliner student
Modification History
Date Action
10/30/15 -- Original version
11/18/15 -- vectors program
12/1/15 -- polymorphism, changing it to use only one vector and pointers
*/
using namespace std;
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
class Student {
protected:
string first, last; // First and laast name
float gpa; // Grade point average
int hoursRegistered; // Number of hours registered for next semester
string major; // Declared major or "Undeclared
static float hourlyRate; // What the college charges per credit hour
static float fees; // The flat fees charged to each student
static float costPerMile; // Cost per mile for travel
public:
// Constructors
Student() {
first = "Unknown";
last = "Person";
gpa = 0.0;
hoursRegistered = 0;
}
Student(string fn, string ln, float gpa, int hours ) {
this->first = fn;
this->last = ln;
this->gpa = gpa;
this->hoursRegistered = hours;
}
// Setters
static void setHourlyRate(float hr) { hourlyRate = hr; }
static void setFees(float f) { fees = f; }
static void setCostPerMile(float cpm) { costPerMile = cpm; }
// Getters
string getMajor() const { return major; }
string getName() const { return first + ' ' + last; }
string getFirst() const { return first; }
string getLast() const { return last; }
int getHoursRegistered() const { return hoursRegistered; }
float getBookCost() const { return 30.00*hoursRegistered ;}
// Input routine
bool read(istream &in) {
in >> first >> last >> gpa >> hoursRegistered >> major;
return in;
}
// Output routine
void write (ostream &out) {
out << first << ' ' << last << ' '
<< gpa << ' ' << hoursRegistered
<< ' ' << major;
}
// estimate -- determine the cost of attending next semester
virtual void estimate(ofstream thisOut)
{
}
};
// Declare location of static variables as globals as required by C++
// These are variables that are shared by all instances of class Student
// and its descendants
float Student::hourlyRate;
float Student::fees;
float Student::costPerMile;
// Class Resident -- extends Student
class Resident : public Student {
protected:
float mealPlan;
float housing;
public:
bool read(istream &in) {
Student::read(in);
in >> mealPlan >> housing;
return in;
}
void write (ostream &out) {
Student::write(out); // Call the write routine inherited from Student
out << ' ' << mealPlan << ' ' << housing;
}
virtual void estimate(ofstream thisOut) {
thisOut << "Dear " + first + ' ' + last + ',' << endl << endl << "You are registered for " << hoursRegistered << " hours. Your costs for the upcoming year have been calculated as follows:" << endl;
thisOut << "Tuition: " << fixed << '$' << (hoursRegistered * hourlyRate) << endl;
thisOut << (hoursRegistered * hourlyRate) + fees + mealPlan + housing;
}
};
// Class Commuter -- extends Student
class Commuter : public Student {
private:
// Must contain miles , mealplan
float miles;
float mealplan;
public:
bool read(istream &in) {
Student::read(in);
in >> mealplan >> miles;
return in;
}
void write (ostream &out) {
Student::write(out);
out << ' ' << mealplan << ' ' << miles;
}
virtual void estimate(ofstream thisOut) {
thisOut << "Dear " + first + ' ' + last + ',' << endl << endl << "You are registered for " << hoursRegistered << " hours. Your costs for the upcoming year have been calculated as follows:" << endl;
thisOut << "Tuition: " << fixed << '$' << (hoursRegistered * hourlyRate) << endl;
thisOut << (hoursRegistered * hourlyRate) + (miles * costPerMile) + fees + mealplan;
}
};
// Class Onliner -- extends Student
class Onliner : public Student {
private:
// Must contain ispcost
float ispcost;
public:
bool read(istream &in) {
Student::read(in);
in >> ispcost;
return in;
}
void write (ostream &out) {
Student::write(out);
out << ispcost;
}
virtual void estimate(ofstream thisOut)
{
thisOut << "Dear " + first + ' ' + last + ',' << endl << endl << "You are registered for " << hoursRegistered << " hours. Your costs for the upcoming year have been calculated as follows:" << endl;
thisOut << "Tuition: " << fixed << '$' << (hoursRegistered * hourlyRate) << endl;
thisOut << (hoursRegistered * hourlyRate) + fees + ispcost;
}
};
// compareStudents -- returns whether or not s1 is less than s2
bool compareStudents(Student* s1, Student* s2) {
return s1 -> getLast() < s2 -> getLast();
}
int main () {
// Declare locals for holding input
ifstream in ("students.dat");
float hourlyRate, fees, costPerMile;
// Read and store the hourly rate, fees and cost per mile
in >> hourlyRate >> fees >> costPerMile;
Student::setHourlyRate(hourlyRate);
Student::setFees(fees);
Student::setCostPerMile(costPerMile);
// Read student records from the input file
char studentType;
vector <Student *> studentVector;
while (in >> studentType) {
if (studentType == 'R') {
Resident *r = new Resident;
r -> read(in);
studentVector.push_back(r);
}
else if(studentType == 'C') {
Commuter *c = new Commuter;
c->read(in);
studentVector.push_back(c);
}
else if(studentType == 'O') {
Onliner *o = new Onliner;
o->read(in);
studentVector.push_back(o);
}
else { // These two lines will need to be replaced
cout << "error: data in file is not correct" << endl;
}
}
// Sort the entire resident list using the supplied comparison routine
sort(studentVector.begin(), studentVector.end(), compareStudents);
// Write the vectors to their respective files
ofstream out;
out.open("students.dat");
for (auto ptr: studentVector) {
ptr -> estimate(out);
}
out.close();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire