嗨,我正在学习 c++ 课程,我们正在学习如何使用用户定义的类,但我的 bool 有问题。到目前为止,我能达到的最远距离是当我编译时出现错误
“没有匹配函数调用 StudRd::ValidRd() 注意:候选人是:bool StudRd::ValidRd(StudRd&) const”
这不仅是我第一次使用类,也是我第一次使用 bool 函数。我已经解决了这个问题并在谷歌上搜索了几个小时的类似问题,但我觉得我还没有更接近理解它。
#include "stud.h"
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("in.data");
outFile.open("out.data");
if(inFile.fail() || outFile.fail())//Checks the in and out files
{
cout << "input file or output file opening failed" << endl;
}
StudRd rd; //Declares an object of the class
rd.DefaultRd(); //Default constructor
outFile << "*~<Student Exam Report>~*" << endl;
outFile << "Stud id" << setw(10) << "Exam 1" << setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10) << "Avg" << endl;
rd.ReadRd(inFile, rd);
while(inFile){ //Loop until the file runs out of data
rd.ValidRd(rd); //Determines if data read is valid
if(rd.ValidRd()){ //if statement is executed if bool returns valid
rd.ValidCountRd(rd);
rd.findAvg(rd); //Function that figures out an average for the struct
rd.MaxOfAllExams(rd); //finds the highest valid exam
rd.WriteRd(outFile, rd); //Function that writes the struct to the outFile
}
else{
rd.InvalidCountRd(rd);
rd.WriteInvalidRd(outFile, rd);
}
rd.ReadRd(inFile, rd); //function that reads from the infile and assigns to a structure
}
rd.AvgOfAllExams(rd); //finds the average of all valid exams
rd.WriteFooterRd(outFile, rd); //Writes the footer containing max, avg, pass, fail, valid, invalid
outFile << "*** END ***" << endl;
return 0;
}
这是类和函数的实现
#include "stud.h"
void StudRd :: DefaultRd()
{
id = -111;
exam1 = -100;
exam2 = -100;
exam3 = -100;
numValid = 0;
numInvalid = 0;
numPass = 0;
numFail = 0;
}
bool StudRd :: ValidRd(StudRd& rd) const
{
return(111 <= rd.id <= 999 &&
0 <= rd.exam1 <= 100 &&
0 <= rd.exam2 <= 100 &&
0 <= rd.exam3 <= 100);
}
void StudRd :: ValidCountRd(StudRd& rd)
{
rd.numValid++;
}
void StudRd :: InvalidCountRd(StudRd& rd)
{
rd.numInvalid++;
}
void StudRd :: MaxOfAllExams(StudRd& rd)
{
if(rd.examMax < rd.exam1){
rd.examMax = rd.exam1;
}
if(rd.examMax < rd.exam2){
rd.examMax = rd.exam2;
}
if(rd.examMax < rd.exam3){
rd.examMax = rd.exam3;
}
}
void StudRd :: AvgOfAllExams(StudRd& rd)
{
rd.avgOfAllExams = rd.examSum / rd.numValid;
}
void StudRd :: ReadRd(ifstream& inFile, StudRd& rd)
{
inFile >> rd.id >> rd.exam1 >> rd.exam2 >> rd.exam3;
}
void StudRd :: WriteRd(ofstream& outFile, StudRd rd)
{
inFile >> rd.id >> rd.exam1 >> rd.exam2 >> rd.exam3;
}
void StudRd :: WriteRd(ofstream& outFile, StudRd rd)
{
outFile.setf(ios::fixed);
outFile.setf(ios::showpoint);
outFile.precision(2);
outFile << rd.id<< setw(10) << rd.exam1 << setw(10) << rd.exam2 << setw(10) << rd.exam3 << setw(10) << rd.avg << setw(10) << rd.passFail << endl;
}
void StudRd :: WriteInvalidRd(ofstream& outFile, StudRd rd)
{
outFile.setf(ios::fixed);
outFile.setf(ios::showpoint);
outFile.precision(2);
outFile << rd.id << setw(10) << rd.exam1 << setw(10) << rd.exam2 << setw(10) << rd.exam3 << setw(10) << "Invalid Data" << endl;
}
void StudRd :: WriteFooterRd(ofstream& outFile, StudRd rd)
{
outFile.setf(ios::fixed);
outFile.setf(ios::showpoint);
outFile.precision(2);
outFile << ">>> Max of All Valid Exams: " << rd.examMax << endl;
outFile << ">>> Average of All Valid Exams: " << rd.avgOfAllExams << endl;
outFile << endl;
outFile << "Number of Valid Data: " << rd.numValid << " Number of Invalid Data: " << rd.numInvalid << endl;
outFile << "Number of Students Passed: " << rd.numPass << " Number of Students Failed: " << rd.numFail << endl;
}
void StudRd :: findAvg(StudRd& rd)
{
rd.avg = float(rd.exam1 + rd.exam2 + rd.exam3)/float(3);
rd.examSum = rd.examSum + rd.avg;
if(rd.avg < 70){
rd.passFail = "F";
numFail++;
}
else{
rd.passFail = "P";
numPass++;
}
}
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
//Creates the structure Studrd
class StudRd
{
public:
void DefaultRd();
bool ValidRd(StudRd& rd) const;
void ValidCountRd(StudRd& rd);
void InvalidCountRd(StudRd& rd);
void MaxOfAllExams(StudRd& rd);
void AvgOfAllExams(StudRd& rd);
void ReadRd(ifstream& inFile, StudRd& rd);
void WriteRd(ofstream& outFile, StudRd rd);
void WriteInvalidRd(ofstream& outFile, StudRd rd);
void WriteFooterRd(ofstream& outFile, StudRd rd);
void findAvg(StudRd& rd);
private:
int id;
int numValid;
int numInvalid;
int numPass;
int numFail;
float exam1;
float exam2;
float exam3;
float avg;
float examMax;
float avgOfAllExams;
float examSum;
string passFail;
};
我不知道是什么原因造成的,对此有任何帮助,所以我至少可以编译它,这样我就可以做最后的调整,我将非常感激。
如果有人发现错误,他们可以指出错误并解释出了什么问题,这将是非常好的。
也是我第一次提交希望我做对了。