472,811 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 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 3328

<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....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.