473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading >>


Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as.

//code for class1
friend std::istream& operator >> (std::istream& lhs, class1& rhs) {
.....random code here
return lhs:}

//code for class2
private:
class1 jimbo;
friend std::istream& operator >> (std::istream& lhs, class2& rhs) {
lhs>>rhs.jimbo;
return lhs;}

This code gives me a compile error and I just cannot seem to figure it
out. I know that the line lhs>>rhs.jimbo; is incorrect, but I am clueless
as to why? Any hints? Thanks.
Nov 7 '05 #1
4 1880

Don Hedgpeth wrote:
Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as.

//code for class1
friend std::istream& operator >> (std::istream& lhs, class1& rhs) {
....random code here
return lhs:}
This makes no sense the way you've defined it. If it's a friend, then
it's not a member function of the class, but you appear to be defining
it right there. (And of course, if it were a member function, then you
wouldn't include class1& as a parameter). Take a look at the FAQ for
an example of doing it correctly:

http://www.parashift.com/c++-faq-lit...html#faq-15.10

//code for class2
private:
class1 jimbo;
friend std::istream& operator >> (std::istream& lhs, class2& rhs) {
lhs>>rhs.jimbo;
return lhs;}
Same problem here.
This code gives me a compile error and I just cannot seem to figure it
out. I know that the line lhs>>rhs.jimbo; is incorrect, but I am clueless
as to why? Any hints?


See above.

Best regards,

Tom

Nov 7 '05 #2
"Don Hedgpeth" <do******@cs.ut exas.edu> wrote in message
news:Pi******** *************** ********@poopde ck.cs.utexas.ed u...

Here's a question - I'm new to c++ and I have two classes that overload
the >> operator. One class calls the other...such as.

//code for class1
friend std::istream& operator >> (std::istream& lhs, class1& rhs) {
....random code here
return lhs:}
return lhs; }

//code for class2
private:
class1 jimbo;
friend std::istream& operator >> (std::istream& lhs, class2& rhs) {
lhs>>rhs.jimbo;
return lhs;}

This code gives me a compile error
What error?
and I just cannot seem to figure it out. I know that the line
lhs>>rhs.jimbo ; is incorrect,
No, you don't know that.
but I am clueless as to why? Any hints? Thanks.


The following adaptation of your code compiles and
give expected results for me (VC++6.0SP6):

#include <istream>
#include <iostream>

class class1
{
friend std::istream& operator >> (std::istream& lhs, class1& rhs)
{
std::cout << "operator>>(std ::istream& lhs, class1& rhs)\n";
return lhs;
}
};
class class2
{
private:
class1 jimbo;
friend std::istream& operator >> (std::istream& lhs, class2& rhs)
{
std::cout << "operator>>(std ::istream& lhs, class2& rhs)\n";
lhs>>rhs.jimbo;
return lhs;
}
};

int main()
{
class2 c2;
std::cin >> c2;
return 0;
}
-Mike
Nov 7 '05 #3

Mike Wahler wrote:
"Don Hedgpeth" <do******@cs.ut exas.edu> wrote in message
news:Pi******** *************** ********@poopde ck.cs.utexas.ed u...

Here's a question - I'm new to c++ and I have two classes that overload
the >> operator. One class calls the other...such as.

//code for class1
friend std::istream& operator >> (std::istream& lhs, class1& rhs) {
....random code here
return lhs:}


return lhs; }

//code for class2
private:
class1 jimbo;
friend std::istream& operator >> (std::istream& lhs, class2& rhs) {
lhs>>rhs.jimbo;
return lhs;}

This code gives me a compile error


What error?
and I just cannot seem to figure it out. I know that the line
lhs>>rhs.jimbo ; is incorrect,


No, you don't know that.
but I am clueless as to why? Any hints? Thanks.


The following adaptation of your code compiles and
give expected results for me (VC++6.0SP6):

#include <istream>
#include <iostream>

class class1
{
friend std::istream& operator >> (std::istream& lhs, class1& rhs)
{
std::cout << "operator>>(std ::istream& lhs, class1& rhs)\n";
return lhs;
}
};
class class2
{
private:
class1 jimbo;
friend std::istream& operator >> (std::istream& lhs, class2& rhs)
{
std::cout << "operator>>(std ::istream& lhs, class2& rhs)\n";
lhs>>rhs.jimbo;
return lhs;
}
};

int main()
{
class2 c2;
std::cin >> c2;
return 0;
}
-Mike


Oops - I stand corrected.

Best regards,

Tom

Nov 7 '05 #4

"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .

Don Hedgpeth wrote:
Here's a question - I'm new to c++ and I have two classes that overload
the >> operator. One class calls the other...such as.

//code for class1
friend std::istream& operator >> (std::istream& lhs, class1& rhs) {
....random code here
return lhs:}
This makes no sense the way you've defined it. If it's a friend, then
it's not a member function of the class, but you appear to be defining
it right there.


That is perfectly acceptable.
(And of course, if it were a member function, then you
wouldn't include class1& as a parameter). Take a look at the FAQ for
an example of doing it correctly:


What he has is correct (except for the typo: using : instead of ; )

-Mike
Nov 7 '05 #5

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

Similar topics

7
2348
by: ranjit mario noronha | last post by:
Hi, I am trying to overload the ->operator as follows : class foo { public: int a; int operator->() ;
8
1772
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
1
1169
by: iceColdFire | last post by:
Hi, Kindyl guide me way to overload -> operator... I need it in global overload sense... Thanks. a.a.cpp
4
2627
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble with operator precedence that I do not know how to overcome.
11
2083
by: Ashwin | last post by:
hi guys, i have overloaded >operator for my own string class .the function is as given below istream& operator>>(istream &in,string1 &s) { char *a; a=new char; in>>a; s=a;
2
2506
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly meant for formatted text input and output? Let's imagine, for example, that I had a standard Windows BMP file (I am aware that C++ has no concept of a BMP - this is simply putting my question in a simple context). If I wanted to load it into a...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
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
9121
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...
0
5498
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...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
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.