2
#include <cstring>
using namespace std;
class Student
{private:
    char _name[20];
    char _no[20];
    int _score;
public:
    Student(int score){_score = score;}
    bool operator>(Student s);
};

bool Student::operator>(Student s, Student b)
{   return s._score > b._score;
};

int main() {
    Student a(70);
    Student b(75);
    cout<<(b>a)<<endl;
}

I just started on C++ and have a problem with simple codes. The error then says that 'bool Student::operator>(Student, Student)' must take exactly one argument.

Oscar
  • 23
  • 2

2 Answers2

3

Operators inside the class take their first argument (if there are two arguments) from the class object itself.

Put the operator outside the class

bool operator>(Student s, Student b)
{   
return s._score > b._score;
};

Or, the operator inside:

bool Student::operator>(Student b)
{  
 return _score > b._score;
};
Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
1

You declared the operator as a class method with 1 argument and the definition must match:

bool Student::operator>(Student rhs)
{   return _score > rhs._score;
};

Not related to your question: you should definitely use std:: string instead of C char arrars

bolov
  • 72,283
  • 15
  • 145
  • 224