Link to home
Start Free TrialLog in
Avatar of rsalvati
rsalvatiFlag for United States of America

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:
#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

Open in new window


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
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rsalvati

ASKER

So you can't over load / as member function the same you would * ?
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  I understand now, I wasn't overloading multiply, but the the pointer dereference.