How do I reference a static function to pass as a parameter?
I have a static function callback
:
static SCDynamicStoreCallBack callback( [params] ){ ... }
In main
, I'm calling
createIPAddressListChangeCallbackSCF(callback, manager, &storeRef, &sourceRef);
This function requires a callback function to be passed as a parameter. However when I try and compile, I get the error
error: ‘callback’ was not declared in this scope
callback
is declared in the root of the file开发者_运维知识库. How should I be referencing it from main
?
I think the problem is that callback() is not defined in the same file as main().
static functions (and variables) aren't visible across files, even if there's a prototype or extern declaration. So, either callback() has to move to the same file as main(), or it has to lose its static'ness.
If both functions are in the same file, either callback() has to be defined first or there must be a prototype/declaration of it before main().
Quoting Wikipedia:
In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.
This means, that the static
function is only visible in the file it is declared. If that is not the same file where you call createIPAddressListChangeCallbackSCF
you run into that exact error. Try it by removing the static
keyword.
EDIT: also add the function definition somewhere readable in your main
.
精彩评论