View previous topic :: View next topic |
Author |
Message |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
Overloading function problem |
Posted: Tue Nov 29, 2011 10:07 am |
|
|
I´m using function overloading.
One of them return an int value. Another function does not return(and does not give me errors)
Compiler CCS 4.124
PIC18F26J11
Code: | void RF24_comm(int comm) {
delay_ms(1);
}
//
int RF24_comm(int comm) {
int rv;
delay_ms(1);
return rv;
}
/************************************/
//Than I use the function overloaded
int RF24_TX_DATA() { //blablabla
int data;
//
// The line below is compiled without errors!
RF24_comm(R_REGISTER|CONFIGURATION); //Read RF24 config register
//
// The line below causes the error:
[b] // *** Error 51 "..." Line 262(43,44): A numeric expression must appear here
[/b] // 1 Errors, 0 Warnings.
data=RF24_comm(R_REGISTER|CONFIGURATION); //Read RF24 config register
delay_ms(1);
} |
Why this is happening?
I don´t know what to do.
It´s urgent. Thanks a lot! _________________ Eduardo Guilherme Brandt |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Nov 29, 2011 10:31 am |
|
|
Oh dear
Overloaded functions are not part of C, they are C++. CCS C supports some overloading however. As far as I can see its because CCS chose to implement part of its maths library in C rather than as intrinsic functions in the compiler. Because functions such as tan(), abs() and so on can take different types of arguments the compiler has to support overloading to get them to work. If you look in math.h you'll see all the overloads of the various maths functions. You can see exactly how they work too. HOWEVER overloading is something that CCS does primariliy for its own use, it is NOT in C.
Overloading stopped working in V4.125 and seems to be working in 4.127.
In any case, overloading usually only works, in languages that support it, with different PARAMETERS in each overloaded case. C, and C++ and possibly C# don't really care about where any returned value goes. You can choose whether to use it or not, therefore the compiler cannot choose which overload to use based on returned result, only input parameters. Your overloads have the same parameters, therefore the compiler is going to get confused as to which it should be using.
The simple advice is do NOT use overloading. Keep right away from it. Its not C. Its not portable, and you may well see strange results when you use it. If you really do want to use it, then make sure your parameters are clearly different types (names are irrelevant).
Please write C, not try to write C++ and then wonder why it sort of, kind of breaks...ish.
RF Developer |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Tue Nov 29, 2011 10:50 am |
|
|
overloading functions seems so elegant!
Thanks again for advising me! _________________ Eduardo Guilherme Brandt |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Tue Nov 29, 2011 10:58 am |
|
|
You´re right again: Compiler doesn´t care about returning parameter.
I killed the void input function and it compiled without errors, even using the function without getting values from it.
Thanks again _________________ Eduardo Guilherme Brandt |
|
|
|