I am working on a debugging project, have managed to fix all errors but one. The error I am getting is the the expression must have bool type. How do I fix this. Also, I clearly do not know how to format code on this forum, so this will be extremely hard to read - if you want to help me out with that too that would be great. Full code below, but the part that I am getting errors on is:
int main()
{
Student a(111, 2, 3.50), b(222, 1, 3.00);
if (a<b)
{
a.showYear();
cout << " is less than ";
b.showYear();
}]
FULL CODE:
[///DEBUG8-4
//This program creates student objects
//and overloads < to compare student year in school
//got rid of the .h
#include<iostream>
#include<conio.h>
//added using namespace std
using namespace std;
class Student
{
private:
int stuID;
int year;
double gpa;
public:
Student(const int i, const int y, const double g);
void showYear();
//got rid of the & and added operator
Student operator<(const Student &otherStu);
};
Student::Student(const int i, const int y, const double g)
{
stuID = i;
year = y;
gpa = g;
}
void Student::showYear()
{
cout << year;
}
Student Student::operator<(const Student &otherStu)
{
int less = 1;
if (year < otherStu.year)
less = 1;
return(otherStu);
}
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;
getchar();
}]
Hi, meghan.
"year" is private inside "Student", so you can't access it directly.
You need a method (i.e. a member function) which returns the variable value.
However there are other minor issues in your code. If you want you can compare it with the following one, for hints.
I needed to get rid of "conio.h" because it doesn't exist in my Linux compiler, but that's not one of the problem, just do not consider that part and reinsert it if you need it.
I am working on a debugging project, have managed to fix all errors but one. The error I am getting is the the expression must have bool type. How do I fix this.
The < operator has been defined to return a Student object. That doesn't make much sense. You probably should change the return type to bool and return a boolean value instead.
meghan wrote:
Also, I clearly do not know how to format code on this forum, so this will be extremely hard to read - if you want to help me out with that too that would be great.
Select the code and press the <> button. Unfortunately there is a bug that prevents you from using the format buttons when creating a new thread so you either have to put [code] and [/code] around your code manually, or make the post and then edit it in order to be able to use the buttons.