在用户定义的类中,Bool 作为函数 c++ 出现问题
Trouble with Bool in a user defined class as a function c++
嗨,我正在学习一门 c++ 课程,我们正在学习如何使用用户定义的类,但我的布尔值遇到了问题。到目前为止,我能达到的最远的距离是当我编译时出现错误
"没有匹配函数调用 StudRd::ValidRd() 注意:候选人是:bool StudRd::ValidRd(StudRd&) const"
这不仅是我第一次使用类,也是我第一次使用布尔函数。我已经讨论过这个问题,并在谷歌上搜索了几个小时的类似问题,但我觉得我离理解它并不近。
#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;
};
我不知道是什么原因造成的,也不知道对此有任何帮助,所以我至少可以让它编译,这样我就可以进行最后的调整,我将不胜感激。
如果有人发现错误,他们可以指出并解释出了什么问题,那将是非常好的。
也是我第一次提交希望我做对了。
您已将函数声明并定义为:
bool ValidRd(StudRd& rd) const;
^^^^^^^^^^^
但是您尝试将其称为:
if(rd.ValidRd()){
^^^
没有任何参数,显然,由于您没有声明一个不带参数的函数,编译器会抱怨。它找不到名称为 ValidRd()
的函数,该函数不带输入参数。
好的,让我们看看错误消息的内容。 翻译过来,它说"你正在尝试调用一个名为 StdRd::ValidRd 的方法,没有参数。 我能找到的最接近的方法是一个StdRd::ValRD(),它引用了一个StdRd。
现在,查看整个错误消息会很方便,其中包括它看到错误的行的行号(可能在冒号之间,如":36:"),但与此同时,查看您称之为 ValRd 的任何地方。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗