overloading >>

Discussion in 'C++' started by Don Hedgpeth, Nov 7, 2005.

  1. Don Hedgpeth

    Don Hedgpeth Guest

    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.
     
    Don Hedgpeth, Nov 7, 2005
    #1
    1. Advertising

  2. 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-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
     
    Thomas Tutone, Nov 7, 2005
    #2
    1. Advertising

  3. Don Hedgpeth

    Mike Wahler Guest

    "Don Hedgpeth" <> wrote in message
    news:p...
    >
    > 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, Nov 7, 2005
    #3
  4. Mike Wahler wrote:
    > "Don Hedgpeth" <> wrote in message
    > news:p...
    > >
    > > 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, Nov 7, 2005
    #4
  5. Don Hedgpeth

    Mike Wahler Guest

    "Thomas Tutone" <> wrote in message
    news:...
    >
    > 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
     
    Mike Wahler, Nov 7, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,451
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    479
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    455
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    605
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    421
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page