473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Returning references

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
1 2330
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Justin Koivisto | last post by:
I was wondering if there was a way to return a reference of an object that lies within another object... <?php class my_class_1{ var $_db=NULL; function my_class_1(){ $this->_db=new...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.