Solved

Simple C++ Operator Overload Problem

Posted on 2011-10-01
5
676 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
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 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 250 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now