如何引用静态函数作为参数传递?

发布于 2024-12-08 09:57:47 字数 481 浏览 0 评论 0原文

我有一个静态函数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-12-15 09:57:47

我认为问题在于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().

清风挽心 2024-12-15 09:57:47

引用 维基百科

在C编程语言中,static与全局变量一起使用
以及将其范围设置为包含文件的函数。在当地
变量,static用于静态存储变量
分配的内存而不是自动分配的内存。尽管
该语言并不规定任何一种类型的实现
内存,静态分配的内存通常保留在数据中
程序在编译时的段,而自动
分配的内存通常作为瞬态调用堆栈实现。

这意味着,static 函数仅在声明它的文件中可见。如果这与您调用 createIPAddressListChangeCallbackSCF 的文件不同,您就会遇到同样的错误。尝试删除 static 关键字。

编辑:还将函数定义添加到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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文