473,399 Members | 2,774 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,399 software developers and data experts.

g++ errors out Pass by Reference function call in C++ --- Please HELP

Hi,

I came acrosss g++ compile errors whenever I make a function call by
reference and found out from the test program that compiler is
treating the function argument differently when another function call
funcRet()is made which returns the expected argument type for the
function call by reference funcByRef(class A&);

The only way to get around this probelm is to first call the
funcRet(), assign its value to a variable and pass that variable to
the function by reference.
But I have these thousands of places where such calls were made and
thought there would be an easier solution to this rather than going to
each and every single place and split the function call in to two
statements ( class var=funcRet();
funcByRef(var);

For example, see the below test program on Linux with g++ version
3.2.2.
This program just demonstrates the compiler problem but doesn't
reflect the actual function calls in the product that I am working on.

1 #include<iostream>
2
3 using std::cout;
4 using std::cin;
5 using std::endl;
6
7 class ref {
8 public:
9 void getNumber();
10 ref getObj();
11 void neg(ref& Obj);
12
13 //private:
14 int i;
15 };
16
17
18 void ref::getNumber(){
19 cout<<"Please enter a number "<<endl;
20 cin>>this->i;
21 cout<<"Assigned value is "<<this->i<<endl;
22 }
23
24 ref ref::getObj(){
25 return (*this);
26 }
27
28 void ref::neg(ref& Obj){
29
30 Obj.i=-Obj.i;
31 cout<<"In the neg func by reference "<<Obj.i<<endl;
32
33 }
34
35
36 //#define refObj.neg(x) \
37 //{ \
38 //ref tmpObj; refObj.neg((tmpObj=x)); \
39 //}
40
41 int main() {
42
43 ref refObj;
44
45 refObj.getNumber();
46
47 //refObj.neg(tmpObj=refObj.getObj());
48 refObj.neg(refObj.getObj());
49
50 ref tmpObj=refObj.getObj();
51 refObj.neg(tmpObj);
52
53 cout<<"neg of the number in tmpObj is "<<tmpObj.i<<endl;
54 cout<<"neg of the number in refObj is "<<refObj.i<<endl;
55
56 return 0;
57
58 }
When I compile, I get the below compile errors.

ref.cxx: In function `int main()':
ref.cxx:48: no matching function for call to `ref::neg(ref)'
ref.cxx:28: candidates are: void ref::neg(ref&)

You can see that if I call the neg() function with another function
call as as argument, copiler is giving mismatch, but If I split it up
(line 50 and 51), it works fine.
Please let me know any thoughts on this....easier way to get around
this problem as I have 1000's of such function calls in a product
which I am porting from windows to Linux. I wouldn't want to go to
each and every place and split it up into two statements.

Your help is highly appreciated.
Thanks,
Gary.
Jul 22 '05 #1
4 3372

<ma*****@ca.com> wrote in message
news:5a**************************@posting.google.c om...
Hi,

I came acrosss g++ compile errors whenever I make a function call by
reference and found out from the test program that compiler is
treating the function argument differently when another function call
funcRet()is made which returns the expected argument type for the
function call by reference funcByRef(class A&);

You cannot bind a temporary to a non-const reference. It's a rule of C++.
Function return values are kind of temporary.

If you can use a const reference instead

void funcByRef(const A&);

but this might mean even more work than your solution, or may not even be
possible. It depends on the function.
The only way to get around this probelm is to first call the
funcRet(), assign its value to a variable and pass that variable to
the function by reference.
Not the only way, a const reference would be the preferred solution.
But I have these thousands of places where such calls were made and
thought there would be an easier solution to this rather than going to
each and every single place and split the function call in to two
statements ( class var=funcRet();
funcByRef(var);

[snip]
Please let me know any thoughts on this....easier way to get around
this problem as I have 1000's of such function calls in a product
which I am porting from windows to Linux. I wouldn't want to go to
each and every place and split it up into two statements.


The Microsoft Visual C++ compiler gets this wrong. No doubt they thought
they were being helpful.

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2f************@uni-berlin.de...

The Microsoft Visual C++ compiler gets this wrong. No doubt they thought
they were being helpful.


Well it cribs if you disable the language extensions (/Za compiler option).
It's a language extension by MS.
Jul 22 '05 #3
ma*****@ca.com wrote:

[reformatted annoying line numbering & indentation]
class ref
{
public:
void getNumber();
ref getObj();
void neg(ref& Obj);
int i;
};

void ref::getNumber() {
cout<<"Please enter a number "<<endl;
cin>>this->i;
cout<<"Assigned value is "<<this->i<<endl;
}

ref ref::getObj() {
return (*this);
}

void ref::neg(ref& Obj) {
Obj.i=-Obj.i;
cout<<"In the neg func by reference "<<Obj.i<<endl;
}
This is rather bad style: the function does not make any use of
the object. Consider either making it static, or having a free
function:
void neg(ref& Obj)
int main() {
ref refObj;

refObj.getNumber();
refObj.neg(refObj.getObj());

ref tmpObj=refObj.getObj();
refObj.neg(tmpObj);

cout<<"neg of the number in tmpObj is "<<tmpObj.i<<endl;
cout<<"neg of the number in refObj is "<<refObj.i<<endl;

return 0;
}

You can see that if I call the neg() function with another function
call as as argument, copiler is giving mismatch, but If I split it up
(line 50 and 51), it works fine.
Please let me know any thoughts on this....easier way to get around
this problem as I have 1000's of such function calls in a product
which I am porting from windows to Linux. I wouldn't want to go to
each and every place and split it up into two statements.


Your code is a great example of why you can't bind a temporary to a
non-const reference! You should have seen in the output that tmpObj.i
was negative, whereas refObj.i was positive. The code:
refObj.neg(refObj.getObj());
has no effect on refObj (a copy of refObj is passed to neg(), not
the actual refObj, and then the copy is destroyed as soon as it has
been negated). The reason that this rule was added to the language
was precisely to catch this sort of error. If you do want to forge
ahead with this useless code, then you have to explicitly name the
temporary object.
Jul 22 '05 #4
"John Harrison" wrote*:
You cannot bind a temporary to a non-const reference. It's a rule of C++.


What need is there for such a rule*? I understand why a warning should
be issued, since this usually shows that the programmer is not aware of
what is happening. But it may still be convenient in some cases, and I
cannot see what accepting this type of code would break.

Anyway, Sun's jvm developpers should stop using it...
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer,...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
5
by: Peter Proost | last post by:
Hi I got this piece of c# code which runs ok but now I translated it to vb.net (see bottom of post for both pieces) and it keeps erroring out at pi.SetValue(testForm, newValue, x) with this error:...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
2
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.