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. 4 1864
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
"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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ranjit mario noronha |
last post by:
Hi,
I am trying to overload the ->operator as follows :
class foo {
public:
int a;
int operator->() ;
|
by: maths_fan |
last post by:
Can't understand how to overload these operations and for what reason?
|
by: iceColdFire |
last post by:
Hi,
Kindyl guide me way to overload -> operator...
I need it in global overload sense...
Thanks.
a.a.cpp
|
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...
|
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;
| |
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |