我似乎無法在這裏發現錯誤,其他文章對錯誤的答案有點模糊這是我的。我收到這個錯誤,我認爲它與它正在嘗試打開的文件有關。我發佈了整個.cpp文件,因爲我不確定錯誤源自哪裏。錯誤C2248:'std :: basic_ios <_Elem,_Traits> :: basic_ios':無法訪問類中聲明的私人成員
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int openFiles(ifstream inFile, ofstream outFile)
{
inFile.open("finalin.dat");
outFile.open("finalout.dat");
outFile << fixed << showpoint << setprecision(2);
inFile >> fixed >> showpoint >> setprecision(2);
if (!inFile||!outFile)
{
cout << "Problem opening file.";
}
}
void initialize(int countFemale,int countMale,float sumFemaleGPA,float sumMaleGPA)
{
countFemale=0;
countMale=0;
sumFemaleGPA=0;
sumMaleGPA=0;
}
int sumGrades(ifstream inFile, float sumFemaleGPA, float sumMaleGPA,int m,int f)
{
if (!inFile)
{
inFile.open("finalin.dat");
}
char sex;
float grade;
while(!inFile.eof())
{
inFile >> sex >> grade;
switch (sex)
{
case 'f': (sumFemaleGPA + grade);
f++;
break;
case 'm': (sumMaleGPA + grade);
m++;
break;
}
}
}
int averageGPA(float avgfGPA, float avgmGPA, int m, int f, float sumFemaleGPA, float sumMaleGPA)
{
avgfGPA=sumFemaleGPA/f;
avgmGPA=sumMaleGPA/m;
}
int printResults(float avgfGPA, float avgmGPA, ofstream outFile)
{
cout <<"The average GPA of the female students is: "<< avgfGPA << endl;
cout <<"The average GPA of the male students is: "<< avgmGPA;
outFile << "The average GPA of the female students is: "<< avgfGPA << endl;
outFile <<"The average GPA of the male students is: "<< avgmGPA;
}
int main()
{
int countFemale;
int countMale;
float sumFemaleGPA;
float sumMaleGPA;
float avgfGPA;
float avgmGPA;
ifstream inFile;
ofstream outFile;
openFiles(inFile,outFile);
initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
sumGrades(inFile,sumFemaleGPA,sumMaleGPA,countMale,countFemale);
averageGPA(avgfGPA,avgmGPA,countMale,countFemale,sumFemaleGPA,sumMaleGPA);
printResults(avgfGPA,avgmGPA, outFile);
}
另外,我認識到,擁有5個功能一樣,是有點混亂,但是這是我們的教授如何要求它,因爲我們要展示我們的函數知識爲好。
切勿使用'eof'。它不會做你的想法。 –
你可能想通過引用傳遞所有這些變量(如'countFemale'等)。 –