overloading >>

D

Don Hedgpeth

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.
 
T

Thomas Tutone

Don said:
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-lite/input-output.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
 
M

Mike Wahler

Don Hedgpeth said:
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
 
T

Thomas Tutone

Mike said:
return lhs; }


What error?


No, you don't know that.


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
 
M

Mike Wahler

Thomas Tutone said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top