By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,428 Members | 1,472 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,428 IT Pros & Developers. It's quick & easy.

overloading >>

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a

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

P: n/a
"Don Hedgpeth" <do******@cs.utexas.edu> wrote in message
news:Pi*******************************@poopdeck.cs .utexas.edu...

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

P: n/a

Mike Wahler wrote:
"Don Hedgpeth" <do******@cs.utexas.edu> wrote in message
news:Pi*******************************@poopdeck.cs .utexas.edu...

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

P: n/a

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.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 discussion thread is closed

Replies have been disabled for this discussion.