首页 文章

C传递给函数时获取ofstream访问错误

提问于
浏览
1

出于某种原因,使用底部迭代器的ofstream因为某种原因而拒绝访问自身,这与指针有什么关系?老实说,我和我的朋友们一直试图弄清楚这里有什么问题 . 即使是经过多年编码的资深人士也无法帮助我 . 任何帮助表示赞赏!这是错误:

错误6错误C2248:'std :: basic_ofstream <_Elem,_Traits> :: basic_ofstream':无法访问类'std :: basic_ofstream <_Elem,_Traits>'中声明的私有成员

以及intellisense

7智能感知:“std :: basic_ofstream <_Elem,_Traits> :: basic_ofstream(const std :: basic_ofstream <_Elem,_Traits> :: _ Myt&_Right)[with _Elem = char,_Traits = std :: char_traits]”(在线声明) 1034“C:\ Program Files(x86)\ Microsoft Visual Studio 11.0 \ VC \ include \ fstream”)无法访问

/*
   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;
}

1 回答

  • 2

    您将按值传递流到函数中,但无法复制流 .

    将函数定义为

    virtual void estimate(ofstream& thisOut)
    

    代替 .

    为了与C 11及更高版本兼容,还有一些地方需要将 return in 替换为 return in.good() (或 static_cast<bool>(in) ) .

相关问题