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

M

mangi03

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.
 
J

John Harrison

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
 
S

Sharad Kala

John Harrison said:
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.
 
O

Old Wolf

(e-mail address removed) 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.
 
M

Marc

"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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top