我已经被困在这个砖墙上几个小时了,我不知道如何处理我的代码来修复这些错误。我试图通过文本文件从给定的家庭作业得分中获得一个班级平均分。我能够让程序读取文本文件,但我无法获得计算平均值的程序。
以下是错误。
error: no matching function for call to 'readFile'
readFile(showAverage);
^~~~~~~~
pa05.cpp:42:6: note: candidate function not viable: no known conversion from 'int (int, int, int)' to 'ifstream &' (aka 'basic_ifstream<char> &') for 1st argument
void readFile(ifstream &someFile)
到目前为止,这是我的代码:
档案:pa05.cpp
// @author Avery Baumann
// @version October 9 2014
// Programming Assignment 05
// This program calculates students' average
// homework scores and their grades and displays it in a nicely
// formatted table. The data is read from a file in order to display it.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
void readFile(ifstream&); //Function prototype
int showAverage (int homeworkOne, int homeworkTwo, int homeworkThree);
int homeworkAverage;
int homeworkOne;
int homeworkTwo;
int homeworkThree;
int main()
{
ifstream dataIn;
dataIn.open("/home/shared/cs135/pa05data.txt");
if (dataIn.fail())
cout << "Error opening data file.\n";
else
{ readFile(dataIn);
dataIn.close();
}
showAverage (homeworkOne, homeworkTwo, homeworkThree);
return 0;
}
/*******************************************************
* readFile *
* This function reads and displays the contents of the *
* input file whose file stream object is passed to it. *
********************************************************/
void readFile(ifstream &someFile)
{
int homeworkOne;
int homeworkTwo;
int homeworkThree;
string student;
while (someFile)
{ someFile >> student >> homeworkOne >> homeworkTwo >> homeworkThree;
cout << student << " "
<< homeworkOne << " "
<< homeworkTwo << " "
<< homeworkThree << " "
}
}
/*******************************************************
* showAverage *
* This function calculates the average of the three *
* homework scores for each student *
********************************************************/
int showAverage(int homeworkOne, int homeworkTwo, int homeworkThree)
{
int hwAverage;
readFile(showAverage);
hwAverage = (homeworkOne + homeworkTwo + homeworkThree) / 2.0;
cout << hwAverage << " "
<< endl;
}
答案 0 :(得分:0)
问题是你使用的函数void readFile(ifstream&);
采用了fstream
的对象,所以部分
{
int hwAverage;
readFile(showAverage);
hwAverage = (homeworkOne + homeworkTwo + homeworkThree) / 2.0;
cout << hwAverage << " "
<< endl;
}
查看函数readFile
您要为其提供哪些参数。函数showAverage
中的readFile(showAverage);
是什么
您必须将ifstream
对象传递给该函数。
答案 1 :(得分:0)
这是函数原型:
void readFile(ifstream&);
该函数接受ifstream
类型的单个参数。你这样称呼它:
readFile(showAverage);
由于showAverage
是函数而不是ifstream
,因此代码无效。
您的readFile
函数读取文件并将值输出到标准输出。但是当readFile
返回时,读取的值将丢失。您无法再访问它们。
因此,您希望保留这些值。您应该将读取的值放入结构中并从readFile
返回。这样,您就可以在readFile
之外使用值。然后,您可以将该结构传递给showAverage
。