473,624 Members | 2,612 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_rati onal::Rational: :numerator'

Below is the .h file:

#ifndef RATIONAL
#define RATIONAL
#include <iostream>

namespace abrowning_ratio nal{

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.numerato r >> target.denomina tor;
return ins;
}
If i comment out the " ins >> target.numerato r >>
target.denomina tor; " line
the error goes away. Can someone please help me find my way home????

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

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

Below is the .h file:

#ifndef RATIONAL
#define RATIONAL
#include <iostream>

namespace abrowning_ratio nal{

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.numerato r >> target.denomina tor;
return ins;
}
If i comment out the " ins >> target.numerato r >>
target.denomina tor; " line
the error goes away. Can someone please help me find my way home????


Please remove irrelevant parts, then post _the_entire_cod e_ 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_ratio nal::operator>> (std::istream&,
abrowning_ratio nal::Rational&) ':
Rational.h:32: error: 'int abrowning_ratio nal::Rational:: numerator' is
private
Rational.cpp:36 : error: within this context
Rational.h:33: error: 'int abrowning_ratio nal::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.c om 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
4713
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 manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
7
1994
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
6437
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 operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
16
16268
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
2271
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
5
3619
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: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
11
3804
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. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
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 try.catch .block to hand this exception.
2
4426
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 & operator>(std::istream & in, const Complex & a); // the methods correspond to the friend std::istream & operator>(std::istream & in, const Complex & a) { std::cout << "real: ";
15
2003
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, <<, >>, =, <>, <, <=, >, >=, CType, IsTrue, IsFalse. Is there any way can to get rid of this error? C# is working fine for overloading ++, and do I need to change developing language to C#? Any suggestion?
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2612
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 we have to send another system
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.