Reputation: 19
I am facing a compilation error with one g++ version (2.9-gnupro-98r2)for LynxOS-178B 2.2.2, whereas the same code will be compiled without complaints with a newer version of g++, e.g. 4.3.3 for VxWorks 653 2.4.0.2.
The following example illustrates the problem:
int method1(int);
void RefInit(){
int (&rmethod) (int) = method1;
rmethod(5);
return;
}
int method1(int x){
int y = x = 10;
return y;
}
At line int (&rmethod) (int) = method1; for 2.9-gnupro-98r2 I am getting:
../../src/Overloading_13_3_1_6_Initialization_by_conversion_function_for_direct_
reference_binding.cpp(8) : error: cannot declare references to functions; use pointer to function instead
If one compiler version accepts the code it cannot be completely wrong. My guess is that it conforms to the C++ standard, but the older compiler was lacking a proper implementation of the same.
Upvotes: 0
Views: 114
Reputation: 1351
The problem is exactly what was said in the error message.
To solve it, use a pointer to the function:
int method1(int);
void PtrInit(){
int (*rmethod) (int) = &method1;
rmethod(5);
return;
}
int method1(int x){
int y = x = 10;
return y;
}
rmethod(5)
automaticly converts to (*rmethod)(5)
.
Upvotes: 1