C++调试控制台应用程序

C++ Debugging console app

本文关键字:应用程序 控制台 调试 C++      更新时间:2023-10-16

我正在为学校开发一个调试控制台应用程序,在编译该程序时出错。

//DEBUG9-4
//This program creates Student objects
//and overloads < to compare student year in school
#include<iostream>
#include<string>
using namespace std;
class Student
{
private: //MISSING :
int stuID;
int year;
double gpa;
public:
Student(const int i, const int y, const double g); // NO VARIABLES DECLARED HERE
void showYear(); //MISSING THE CURLY BRACES
bool operator < (const Student);
};
Student::Student(const int i, const int y, const double g) // VARIABLES WERE NOT MATCHING
{
stuID = i;
year = i;
gpa = g; // VARIABLE g WAS POST TO BE THE "gpa" VARIABLE
}
void Student::showYear()
{
cout << year;
}
int Student::operator < (const Student otherStu)
{
bool less = false;
if (year < otherStu.year)
less = true;
return less;
}
int main()
{
Student a(111, 2, 3.50), b(222, 1, 3.00);
if(a < b)
{
a.showYear();
cout << " is less than ";
b.showYear();
}
else
{
a.showYear();
cout << " is not less than ";
b.showYear();
}
cout << endl;
return 0;
}

行:28错误:'int Student::operator<的原型;(const Student&otherStu("与课堂上的任何学生都不匹配。行:16错误:候选者为:bool学生::运算符<(学生(。

您想要这样实现它:

bool Student::operator < (const Student otherStu)
{
bool less = false;
if (year < otherStu.year)
less = true;
return less;
}

因为现在你的定义和声明不匹配,它们可能有不同的返回值。

或者你可以只做:

bool Student::operator < (const Student b)
{
return year < b.year ? true : false;
}