Returning references

Discussion in 'C++' started by Karlo Basic, Nov 15, 2003.

  1. Karlo Basic

    Karlo Basic Guest

    Hi!
    I'm still just learning to program in C++ and I've got a question about
    returning references.
    Below is the code of my program. I think it's pretty obvious what it
    does...
    What doesn't work is shown in comments in the code:
    ******************************************************
    #include <iostream>
    using std::cin;
    using std::cout;

    class Koordinate
    {
    public:
    Koordinate() : x(0),y(0){}
    Koordinate(int a,int b) : x(a),y(b){}
    friend Koordinate operator +(Koordinate p1, Koordinate p2){
    Koordinate temp;
    temp.x = p1.x + p2.x;
    temp.y = p1.y + p2.y;
    return temp;
    }
    /*
    friend std::eek:stream& operator <<(std::eek:stream& output, Koordinate
    point)
    If I put the upper function heading instead of the one below then it
    works normal.
    But with the function header below the compiler complains(g++):
    koordinate.cpp: In function 'int main()':
    koordinate.cpp:XX:could not convert 'operator+(point2)' to 'Koordinate
    &'
    koordinate.cpp:XX: in passing argument 2 of 'operator<< (ostream
    &,Koordinate &)'
    */
    friend std::eek:stream& operator <<(std::eek:stream& output, Koordinate&
    point){
    output << "(" << point.x << "," << point.y << ")";
    return output;
    }
    private:
    int x,y;
    };

    int main()
    {
    Koordinate point(4,7), point2(2,3);
    cout << (point + point2) << endl;
    /*
    If I put this instead:
    Koordinate point(4,7), point2(2,3), temp;
    temp = point + point2;
    cout << temp << endl;
    then it works without the need to change anything.
    */
    return 0;
    }
    *********************************************************
    Thank you in advance for your time,
    Karlo.
     
    Karlo Basic, Nov 15, 2003
    #1
    1. Advertising

  2. Karlo Basic

    Rolf Magnus Guest

    Karlo Basic wrote:

    > Hi!
    > I'm still just learning to program in C++ and I've got a question
    > about returning references.
    > Below is the code of my program. I think it's pretty obvious what it
    > does...
    > What doesn't work is shown in comments in the code:
    > ******************************************************
    > #include <iostream>
    > using std::cin;
    > using std::cout;
    >
    > class Koordinate
    > {
    > public:
    > Koordinate() : x(0),y(0){}
    > Koordinate(int a,int b) : x(a),y(b){}
    > friend Koordinate operator +(Koordinate p1, Koordinate p2){
    > Koordinate temp;
    > temp.x = p1.x + p2.x;
    > temp.y = p1.y + p2.y;
    > return temp;
    > }
    > /*
    > friend std::eek:stream& operator <<(std::eek:stream& output, Koordinate
    > point)
    > If I put the upper function heading instead of the one below then it
    > works normal.
    > But with the function header below the compiler complains(g++):
    > koordinate.cpp: In function 'int main()':
    > koordinate.cpp:XX:could not convert 'operator+(point2)' to
    > 'Koordinate
    > &'
    > koordinate.cpp:XX: in passing argument 2 of 'operator<< (ostream
    > &,Koordinate &)'
    > */
    > friend std::eek:stream& operator <<(std::eek:stream& output, Koordinate&
    > point){
    > output << "(" << point.x << "," << point.y << ")";
    > return output;
    > }
    > private:
    > int x,y;
    > };
    >
    > int main()
    > {
    > Koordinate point(4,7), point2(2,3);
    > cout << (point + point2) << endl;


    The result of (point + point2) is a nameless temporary Koordinate
    objects, and it's not possible to bind a non-const reference to a
    temporary in C++. Since your operator doesn't modify the object, that
    parameter should be const anyway, so write your operator as

    std::eek:stream& operator <<(std::eek:stream& output,
    const Koordinate& point)

    > /*
    > If I put this instead:
    > Koordinate point(4,7), point2(2,3), temp;
    > temp = point + point2;
    > cout << temp << endl;
    > then it works without the need to change anything.


    This time, temp is not a temporary, but a named variable, which can be
    bound to a non-const reference.
     
    Rolf Magnus, Nov 16, 2003
    #2
    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. Roger Leigh
    Replies:
    8
    Views:
    458
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    470
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    626
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    733
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    859
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page