如何引用静态函数作为参数传递?
我有一个静态函数callback
:
static SCDynamicStoreCallBack callback( [params] ){ ... }
在main
中,我调用
createIPAddressListChangeCallbackSCF(callback, manager, &storeRef, &sourceRef);
此函数需要将回调函数作为参数传递。但是,当我尝试编译时,出现错误
error: ‘callback’ was not declared in this scope
callback
is statements in the root of the file。我应该如何从 main
引用它?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于callback() 没有与main() 在同一个文件中定义。
静态函数(和变量)在文件中不可见,即使有原型或 extern 声明也是如此。因此,callback() 要么必须移动到与 main() 相同的文件,要么必须失去其静态性。
如果两个函数位于同一个文件中,则必须首先定义callback(),或者在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().
引用 维基百科:
这意味着,
static
函数仅在声明它的文件中可见。如果这与您调用 createIPAddressListChangeCallbackSCF 的文件不同,您就会遇到同样的错误。尝试删除static
关键字。编辑:还将函数定义添加到
main
中可读的位置。Quoting Wikipedia:
This means, that the
static
function is only visible in the file it is declared. If that is not the same file where you callcreateIPAddressListChangeCallbackSCF
you run into that exact error. Try it by removing thestatic
keyword.EDIT: also add the function definition somewhere readable in your
main
.