Connecting Tech Pros Worldwide Forums | Help | Site Map

overloading >>

Don Hedgpeth
Guest
 
Posts: n/a
#1: Nov 7 '05

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.

Thomas Tutone
Guest
 
Posts: n/a
#2: Nov 7 '05

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

Mike Wahler
Guest
 
Posts: n/a
#3: Nov 7 '05

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


Thomas Tutone
Guest
 
Posts: n/a
#4: Nov 7 '05

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

Mike Wahler
Guest
 
Posts: n/a
#5: Nov 7 '05

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


Closed Thread