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

C++ C++;调试控制台应用程序,c++,C++,我正在为学校开发一个调试控制台应用程序,在编译程序时出错 //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 : in

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

//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;
}
//DEBUG9-4
//该程序创建学生对象
//和过载<来比较学生在学校的年数
#包括
#包括
使用名称空间std;
班级学生
{
私人文件://缺失:
int stuID;
国际年;
双gpa;
公众:
Student(const int i,const int y,const double g);//此处未声明任何变量
void showYear();//缺少花括号
布尔运算符<(常数学生);
};
Student::Student(const int i,const int y,const double g)//变量不匹配
{
stuID=i;
年份=i;
gpa=g;//变量g被后置为“gpa”变量
}
无效学生::showYear()
{

您想这样实现它吗

bool Student::operator < (const Student otherStu)
{
   bool less = false;
   if (year < otherStu.year)
      less = true;
   return less;
}
bool Student::operator<(const Student otherStu)
{
bool-less=false;
如果(年<其他研究年)
小于等于真;
回报少;
}
因为现在您的定义和声明不匹配,所以它们有不同的返回值

或者你可以这样做:

bool Student::operator < (const Student b)
{
   return year < b.year ? true : false;
}
bool Student::operator<(const Student b)
{
返回年份
错误消息对我来说似乎很清楚-您以一种方式声明了函数,但以另一种方式实现了它。声明和定义应该匹配。您遇到的是编译(或构建)错误。这与调试无关(在运行时完成)。请再看一看
Student::Student(常数i,常数y,常数双g)
。这里可能有编译器警告。不要忽略编译器警告。编译器错误会阻止编译器创建程序,但编译器警告会告诉您,程序可能无法按您希望的方式运行。编译器警告是您防止简单错误的第一道防线,因此请将它们大声打开并注意注意:如果没有编译器警告,请在g++命令中添加-Wall,并注意函数中没有使用
y
,这在上下文中没有意义。这是一个有趣的示例,其中命名内容会对可读性产生负面影响,而不是
返回年份