446,260 Members | 1,233 Online Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

# Returning references

 P: n/a 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 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::ostream& operator <<(std::ostream& 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::ostream& operator <<(std::ostream& 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. Jul 19 '05 #1 