gpt4 book ai didi

c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class

转载 作者:行者123 更新时间:2023-11-30 00:57:21 25 4
gpt4 key购买 nike

我似乎无法发现这里的错误,其他文章对错误的答案有点模糊所以这里是我的。我收到此错误,我相信它与它试图打开的文件有关。我发布了整个 .cpp 文件,因为我不确定错误的来源。

#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 个这样的函数有点乱,但这是我们教授的要求,因为我们也要展示我们对函数的了解。

最佳答案

问题是你不能按值传递流,你必须按引用或指针传递它们。在每个函数参数定义中,在流之后添加 &,例如:

int printResults(float avgfGPA, float avgmGPA, ofstream& outFile)

代替

int printResults(float avgfGPA, float avgmGPA, ofstream outFile)

编辑:
您的初始化不执行任何操作,因为它按值获取参数。您需要通过引用来获取它们才能修改源代码,使用

void initialize(int &countFemale,int &countMale,float &sumFemaleGPA,float &sumMaleGPA)

代替

void initialize(int countFemale,int countMale,float sumFemaleGPA,float sumMaleGPA)

并且警告应该消失。

关于c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8425260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com