rsalvati
asked on
Simple C++ Operator Overload Problem
I'm trying to overload the division operator in C++ for a class assignment. The +, - and * are all fine, but I get a weird compilation error for division. I'm using Visual C++ 2010 Express.
Header file:
Compilation error I get is:
RationalNumber.h(10): error C2805: binary 'operator /' has too few parameters
Note: if I comment out the / overload, all the other 3 compile fine.
Header file:
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
using namespace std;
class RationalNumber
{
friend RationalNumber operator+ (RationalNumber);
friend RationalNumber operator- (RationalNumber);
friend RationalNumber operator/ (RationalNumber);
friend RationalNumber operator* (RationalNumber);
public:
RationalNumber(int numerator, int denominator);
System::String^ toString();
int getNumerator();
int getDenominator();
RationalNumber operator+ (const RationalNumber& r) const;
RationalNumber operator- (const RationalNumber& r) const;
RationalNumber operator/ (const RationalNumber& r) const;
RationalNumber operator* (const RationalNumber& r) const;
private:
int _numerator;
int _denominator;
int _wholeNumber;
bool isValidDenominator(const int denominator);
void simplifyFraction();
};
#endif
Compilation error I get is:
RationalNumber.h(10): error C2805: binary 'operator /' has too few parameters
Note: if I comment out the / overload, all the other 3 compile fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No, since it is binary. See http://msdn.microsoft.com/en-us/library/5tk49fh2%28VS.80%29.aspx ("Operator Overloading") for a list of operators you can overload and their types.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! I understand now, I wasn't overloading multiply, but the the pointer dereference.
ASKER