I have to overload parameters and I started with the "<" parameter to compare two classes together. I have this as my code but it keeps saying that the function bool Money :: operator<(const Money &, const Money&) must take exactly one argument. Why can't I pass both? is there a more efficient way of doing this?
There are values already in both classes. If anyone can steer me the right direction. Thanks
1 2 3 4 5 6 7 8 9 10
bool Money::operator<(const Money &m,const Money &d)
{
int a = m.getValue();
int b = d.getValue();
cout << a << b << endl; /// debugging
return a < b;
}
If you have a const object, you can only call const functions (since non-const functions might modify the state of that object).
If getValue is a non-const function, then you won't be able to call it from your < operator. You'll need to make it const:
1 2 3 4 5 6 7
int Money::getValue() const // <- note the 'const' keyword
{
// ...
}
bool Money::operator < (const Money& d) const // <- also do that with the < operator, too
// I messed up and forgot about it before. Whoops.