Connecting Tech Pros Worldwide Help | Site Map

overloading >>

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 7th, 2005, 12:15 AM
Don Hedgpeth
Guest
 
Posts: n/a
Default 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.

  #2  
Old November 7th, 2005, 12:35 AM
Thomas Tutone
Guest
 
Posts: n/a
Default Re: overloading >>


Don Hedgpeth wrote:[color=blue]
> 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:}[/color]

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
[color=blue]
>
> //code for class2
> private:
> class1 jimbo;
> friend std::istream& operator >> (std::istream& lhs, class2& rhs) {
> lhs>>rhs.jimbo;
> return lhs;}[/color]

Same problem here.
[color=blue]
> 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?[/color]

See above.

Best regards,

Tom

  #3  
Old November 7th, 2005, 12:35 AM
Mike Wahler
Guest
 
Posts: n/a
Default Re: overloading >>

"Don Hedgpeth" <donhedge@cs.utexas.edu> wrote in message
news:Pine.LNX.4.63.0511061904200.10324@poopdeck.cs .utexas.edu...[color=blue]
>
> 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:}[/color]

return lhs; }
[color=blue]
>
> //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[/color]

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

No, you don't know that.
[color=blue]
>but I am clueless as to why? Any hints? Thanks.[/color]

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


  #4  
Old November 7th, 2005, 12:35 AM
Thomas Tutone
Guest
 
Posts: n/a
Default Re: overloading >>


Mike Wahler wrote:[color=blue]
> "Don Hedgpeth" <donhedge@cs.utexas.edu> wrote in message
> news:Pine.LNX.4.63.0511061904200.10324@poopdeck.cs .utexas.edu...[color=green]
> >
> > 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:}[/color]
>
> return lhs; }
>[color=green]
> >
> > //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[/color]
>
> What error?
>[color=green]
> >and I just cannot seem to figure it out. I know that the line
> >lhs>>rhs.jimbo; is incorrect,[/color]
>
> No, you don't know that.
>[color=green]
> >but I am clueless as to why? Any hints? Thanks.[/color]
>
> 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[/color]

Oops - I stand corrected.

Best regards,

Tom

  #5  
Old November 7th, 2005, 12:35 AM
Mike Wahler
Guest
 
Posts: n/a
Default Re: overloading >>


"Thomas Tutone" <Thomas8675309@yahoo.com> wrote in message
news:1131326553.851060.217580@g47g2000cwa.googlegr oups.com...[color=blue]
>
> Don Hedgpeth wrote:[color=green]
>> 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:}[/color]
>
> 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.[/color]

That is perfectly acceptable.
[color=blue]
> (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:[/color]

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

-Mike


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.