Solved

Simple C++ Operator Overload Problem

Posted on 2011-10-01
5
689 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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