C++ C++;传递给函数时获取流访问错误
出于某种原因,底部带有迭代器的Outstream出于某种原因拒绝了对自身的访问,与指针有关吗?老实说,我和我的朋友们一直在努力找出这里出了什么问题。即使是一位多年从事编码工作的老手也帮不了我。感谢您的帮助!以下是错误: 错误6错误C2248:'std::basic_of stream::basic_of stream':无法访问在类'std::basic_of stream'中声明的私有成员 还有智能感知 7智能感知:“std::basic_of stream::basic_of stream(const std::basic_of stream::_Myt&_Right)[with _Elem=char,_Traits=std::char_Traits]”(在“C:\Program Files(x86)\Microsoft Visual Studio 11.0\VC\include\fstream”的第1034行声明)不可访问C++ C++;传递给函数时获取流访问错误,c++,c++11,C++,C++11,出于某种原因,底部带有迭代器的Outstream出于某种原因拒绝了对自身的访问,与指针有关吗?老实说,我和我的朋友们一直在努力找出这里出了什么问题。即使是一位多年从事编码工作的老手也帮不了我。感谢您的帮助!以下是错误: 错误6错误C2248:'std::basic_of stream::basic_of stream':无法访问在类'std::basic_of stream'中声明的私有成员 还有智能感知 7智能感知:“std::basic_of stream::basic_of stream(
/*
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;
}
/*
estimate.cc——学生出勤成本估算程序——第1部分
这部分将读取记录,将它们放在单独的向量中,对它们进行排序,
然后把它们写在不同的文件中。
作者:拉里·莫雷尔,
艾伦·威尔逊
战略:
为名为student的普通学生创建一个类
将学生分为三类:居民、通勤者、在线者。
为这些类别中的每一个设置一个类以从Student继承。
输入文件名为students.dat。其格式如下。
小时费率百分位数——第一行
其余的行是三种格式之一,每个学生一行
R名字姓氏GPA小时注册主要MealPlan住房——为留校学生提供
C名字姓氏GPA小时注册主要英里数MealPlan——适用于通勤学生
O名字姓氏GPA小时注册主要ISP_成本-在线学生
修改历史
日期动作
2015年10月30日——原版
2015年11月18日——矢量程序
12/1/15——多态性,将其更改为仅使用一个向量和指针
*/
使用名称空间std;
#包括
#包括
#包括
#包括
#包括
#包括
班级学生{
受保护的:
字符串first,last;//first和last name
浮点gpa;//平均成绩点
int hoursregisted;//下学期注册的学时数
string major;//已声明的major或“未声明”
静态浮动小时率;//学院每学分收费多少
静态浮动费用;//向每个学生收取的固定费用
静态浮动成本;//每英里旅行成本
公众:
//建设者
学生(){
first=“未知”;
last=“Person”;
gpa=0.0;
注册小时数=0;
}
学生(字符串fn、字符串ln、浮动gpa、整数小时){
这个->第一个=fn;
这个->最后一个=ln;
这->gpa=gpa;
此->小时注册=小时;
}
//二传手
静态void setHourlyRate(float hr){hourlyRate=hr;}
静态无效设置费用(浮动f){fees=f;}
静态void setCostPerMile(float cpm){costPerMile=cpm;}
//吸气剂
字符串getMajor()常量{return major;}
字符串getName()常量{return first+''+last;}
字符串getFirst()常量{return first;}
字符串getLast()常量{return last;}
int getHoursRegistered()常量{return hoursRegistered;}
float getBookCost()常量{return 30.00*小时已注册;}
//输入程序
布尔读取(istream&in){
在>>第一>>最后>>平均成绩>>小时注册>>专业;
返回;
}
//输出程序
无效写入(ostream&out){
out您正在按值将流传递到函数中,但无法复制流
将函数定义为
virtual void estimate(ofstream& thisOut)
相反
为了与C++11及更高版本兼容,还有一些地方需要将return-in
替换为return-in.good()
(或static_-cast(in)
)