Solved

Simple C++ Operator Overload Problem

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception thrown at 0x00007FFD5BC81F28 7 49
VS2015 Redefinition errors 4 68
How do i run a c++ file? 15 46
Where are the c++ header files to use in Eclipse? 4 24
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

856 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