470,815 Members | 1,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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 2428
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Terje Slettebř | last post: by
7 posts views Thread by Hendrik Schober | last post: by
31 posts views Thread by | last post: by
5 posts views Thread by Jerry Fleming | last post: by
11 posts views Thread by jakester | last post: by
14 posts views Thread by Pramod | last post: by
2 posts views Thread by Colonel | last post: by
15 posts views Thread by =?Utf-8?B?VkIgRGV2ZWxvcGVy?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.