?
Solved

Simple C++ Operator Overload Problem

Posted on 2011-10-01
5
Medium Priority
?
723 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:rsalvati
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 36897242
The key is 'binary operator' - you should use
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

using namespace std;

// foward declaration
RationalNumber operator/ (const RationalNumber& l, const RationalNumer& r);	

class RationalNumber
{
	// 'friend' is only necessary for non-members	
	friend RationalNumber operator/ (const RationalNumber& l, const RationalNumer& r);	
		

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

// ...



RationalNumber operator/ (const RationalNumber& l, const RationalNumer& r);
{
  // implement here
}

Open in new window

0
 

Author Comment

by:rsalvati
ID: 36897288
So you can't over load / as member function the same you would * ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 36897335
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.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 36897431
>> So you can't over load / as member function the same you would * ?

You can, but those friend declarations aren't for overloading the operators you think.

You overloaded the unary minus, the unary plus, and the dereference operator, NOT the binary minus, binary plus and binary multiplication operators like you intended.
0
 

Author Closing Comment

by:rsalvati
ID: 36897528
Thank you!  I understand now, I wasn't overloading multiply, but the the pointer dereference.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question