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