Overloading < For Class
Apr 26, 2014 at 2:25am UTC
I thought this would be very simple, yet after searching forever and seemingly doing it right according to the sources I can find...it's not working. Here is what I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#ifndef DB_STUDENT
#define DB_STUDENT
#include <string.h>
using namespace std;
class Student {
private :
int ID;
string name;
public :
Student(int entry);
virtual ~Student();
const bool operator < (const Student& rhs) const ;
};
Student::Student(int entry) {
ID = entry;
}
Student::~Student() {
}
const bool Student::operator < (const Student& rhs) const { return ID < rhs.ID; }
#endif
Then, in my main.cpp I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "Student.h"
using namespace std;
int main(int argc, char * argv[])
{
Student* stud = new Student(5);
Student* stud2 = new Student(2);
if (stud < stud2) { cout << "Yes" ; }
return 0;
}
It is printing out the Yes, despite stud's ID clearly not being less than stud2's. What am I doing wrong?
Last edited on Apr 26, 2014 at 2:28am UTC
Apr 26, 2014 at 3:05am UTC
*stud < *stud2
Apr 26, 2014 at 4:13am UTC
Oh...yeah. Thanks!
Topic archived. No new replies allowed.