By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
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 <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::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
Share this Question
Share on Google+
1 Reply


P: n/a
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::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;
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::ostream& operator <<(std::ostream& 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.

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.