473,498 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading error

I am getting an istream overloading error that looks like this:

error: no match for 'operator>>' in 'ins >>
target->abrowning_rational::Rational::numerator'

Below is the .h file:

#ifndef RATIONAL
#define RATIONAL
#include <iostream>

namespace abrowning_rational{

class Rational{

public:

//CONSTRUCTOR for the rational class
Rational();
Rational(int a, int b);

//CONSTANT MEMBER FUNCTIONS for the Rational class
int getNum () const {return numerator;} // Gets numerator
int getDen () const {return denominator;} // Gets denominator

//MODIFICATION MEMBER FUNCTIONS for the rational class
int gcd(int a, int b); // returns GCD using euclidean algorithm

//Friend Functions
friend std::istream& operator >> (std::istream& ins, const
Rational& target);

private:

int numerator;
int denominator;

};

//NONMEMBER FUNCTIONS for the Rational class
Rational operator + (const Rational& r1, const Rational& r2);
Rational operator - (const Rational& r1, const Rational& r2);
Rational operator * (const Rational& r1, const Rational& r2);
Rational operator / (const Rational& r1, const Rational& r2);
bool operator == (const Rational& r1, const Rational& r2);
bool operator != (const Rational& r1, const Rational& r2);
bool operator >= (const Rational& r1, const Rational& r2);
bool operator <= (const Rational& r1, const Rational& r2);
bool operator > (const Rational& r1, const Rational& r2);
bool operator < (const Rational& r1, const Rational& r2);
std::ostream& operator << (std::ostream& outs, const Rational&
source);

}

#endif //RATIONAL_H
~
And now the implementation of the overloaded input operator:

std::istream& operator >> (std::istream& ins, const Rational& target){
//Postcondition: target is read from ins. the return value is
//istream ins. Library facility used is iostream

ins >> target.numerator >> target.denominator;
return ins;
}
If i comment out the " ins >> target.numerator >>
target.denominator; " line
the error goes away. Can someone please help me find my way home????

Mar 21 '06 #1
8 2580
andrew browning wrote:
I am getting an istream overloading error that looks like this:

error: no match for 'operator>>' in 'ins >>
target->abrowning_rational::Rational::numerator'

Below is the .h file:

#ifndef RATIONAL
#define RATIONAL
#include <iostream>

namespace abrowning_rational{

class Rational{

public:

//CONSTRUCTOR for the rational class
Rational();
Rational(int a, int b);

//CONSTANT MEMBER FUNCTIONS for the Rational class
int getNum () const {return numerator;} // Gets numerator
int getDen () const {return denominator;} // Gets denominator

//MODIFICATION MEMBER FUNCTIONS for the rational class
int gcd(int a, int b); // returns GCD using euclidean algorithm

//Friend Functions
friend std::istream& operator >> (std::istream& ins, const
Rational& target);

private:

int numerator;
int denominator;

};

//NONMEMBER FUNCTIONS for the Rational class
Rational operator + (const Rational& r1, const Rational& r2);
Rational operator - (const Rational& r1, const Rational& r2);
Rational operator * (const Rational& r1, const Rational& r2);
Rational operator / (const Rational& r1, const Rational& r2);
bool operator == (const Rational& r1, const Rational& r2);
bool operator != (const Rational& r1, const Rational& r2);
bool operator >= (const Rational& r1, const Rational& r2);
bool operator <= (const Rational& r1, const Rational& r2);
bool operator > (const Rational& r1, const Rational& r2);
bool operator < (const Rational& r1, const Rational& r2);
std::ostream& operator << (std::ostream& outs, const Rational&
source);

}

#endif //RATIONAL_H
~
And now the implementation of the overloaded input operator:

std::istream& operator >> (std::istream& ins, const Rational& target){
//Postcondition: target is read from ins. the return value is
//istream ins. Library facility used is iostream

ins >> target.numerator >> target.denominator;
return ins;
}
If i comment out the " ins >> target.numerator >>
target.denominator; " line
the error goes away. Can someone please help me find my way home????


Please remove irrelevant parts, then post _the_entire_code_ that fails
to compile. The reason is most likely in the fact that you declare the
friend function one way and define it some other way. Since you don't
show how (or, rather, where), nothing special can be said about it.

V
--
Please remove capital As from my address when replying by mail
Mar 21 '06 #2
- Remove the 'const' from the second parameter of the operator >>()
definition.
- Declare operator >> as friend of Rational otherwise it will not have
access to its 'numerator' and 'denominator' members which are private.

Mar 21 '06 #3
Ok you already have the friend, so just remove the const ;)

Mar 21 '06 #4
thanks. i removed the const but then got an errors saying the
numerator and denominator were private and that there was an error
within that context:

Rational.cpp: In function 'std::istream&
abrowning_rational::operator>>(std::istream&,
abrowning_rational::Rational&)':
Rational.h:32: error: 'int abrowning_rational::Rational::numerator' is
private
Rational.cpp:36: error: within this context
Rational.h:33: error: 'int abrowning_rational::Rational::denominator'
is private
Rational.cpp:36: error: within this context

Mar 21 '06 #5
Did you also remove the const from the friend declaration?

Mar 21 '06 #6
just did and it rocked!!!! thanks ever so much for your help, it was
very gracious of you

Mar 21 '06 #7
tm*****@gmail.com wrote:
Did you also remove the const from the friend declaration?


Read this:

http://cfaj.freeshell.org/google/

Brian
Mar 21 '06 #8
andrew browning wrote:
just did and it rocked!!!! thanks ever so much for your help, it was
very gracious of you

Read this:

http://cfaj.freeshell.org/google/

Brian
Mar 21 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4690
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
7
1982
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
34
6402
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
16192
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
31
2235
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
5
3602
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
11
3789
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
14
562
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use...
2
4396
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
15
1993
by: =?Utf-8?B?VkIgRGV2ZWxvcGVy?= | last post by:
Overloading of the unary ++ operator in vb.net is not working. It show error: Operator declaration must be one of: +, -, *, \, /, ^, &, Like, Mod, And, Or, Xor, Not, <<, >>, =, <>, <, <=, >, >=,...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.