在 FSEventStreamCreate 中声明回调函数
有几个使用 FSEvent 侦听文件系统中的更改的示例。
如何监听文件系统更改 MAC - kFSEventStreamCreateFlagWatchRoot
和
使用 创建事件时FSEventStreamCreate
他们似乎都很好地传递了回调项。没有参数或任何东西,只有 &feCallback
。基本上,他们似乎传递的是变量而不是函数,如果这有意义的话。
但当我尝试执行此操作时,出现使用未声明的标识符
错误。什么给?
FSEventStreamRef stream = FSEventStreamCreate(NULL,
&feCallback, // what does '&' mean? Why are no parameters passed?
&cntxt,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
1,
kFSEventStreamCreateFlagWatchRoot );
然后再有回调函数:
static void feCallback(ConstFSEventStreamRef streamRef,
void* pClientCallBackInfo,
size_t numEvents,
void* pEventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
NSLog(@"The file changed!");
}
我想要一些示例代码来让开源帮助器对象在这里工作: https://bitbucket.org/boredzo/fs-notifier/overview
但同样的事情。它具有方法:
- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath;
由于错误,我无法向其传递 newCallback
如上所述。
There are a couple examples of using an FSEvent to listen for changes in the file system.
How to listen for file system changes MAC - kFSEventStreamCreateFlagWatchRoot
and
FSEvents weirdness on OS X Leopard
When creating the event with FSEventStreamCreate
they all seem to pass the callback item just fine. No parameters or anything, just &feCallback
. Basically it seems as if they're passing a variable rather than a function, if that makes sense.
But I'm getting a Use of Undeclared identifier
error when I try to do it. What gives?
FSEventStreamRef stream = FSEventStreamCreate(NULL,
&feCallback, // what does '&' mean? Why are no parameters passed?
&cntxt,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
1,
kFSEventStreamCreateFlagWatchRoot );
and then later have the callback function:
static void feCallback(ConstFSEventStreamRef streamRef,
void* pClientCallBackInfo,
size_t numEvents,
void* pEventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
NSLog(@"The file changed!");
}
I'd love some sample code to get the open-source helper object here working: https://bitbucket.org/boredzo/fs-notifier/overview
But same thing. It has the method:
- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath;
and I can't pass it a newCallback
because of the error described above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
&
是“地址”运算符,其计算结果是指向其操作数的指针。这里其实没有必要;不在函数调用中的函数名始终计算为指向该函数的指针,无论是否带有&
。因此,这会将指针传递给
feCallback
函数,这就是 FSEventStream 对象回调它的方式。这确实有道理,但不,这是不正确的。他们正在传递函数本身。
可以声明一个包含指向函数的指针的变量。如果
feCallback
是这样一个变量,那么feCallback
和&feCallback
将意味着不同的事情:feCallback
将是指向函数的指针(在feCallback
变量内),而&feCallback
将是指向变量的指针。不过,在您的情况下,由于
feCallback
是一个函数,这两个表达式是等效的;无论哪种方式都会传递函数指针。您尚未声明该标识符(在本例中为该函数)。
这就是问题所在。您在使用其名称后定义了回调函数,但在使用其名称之前没有声明该函数。您必须先声明该函数,然后才能使用它(或将其传递到任何地方)。
原因是编译器不知道“
feCallback
”是什么,直到你告诉它,这就是声明的作用。当您在声明或定义之前尝试引用“feCallback
”时,编译器不知道您在说什么。另一种方法是将函数的定义向上移动。定义算作其后所有内容的声明。不过,我会将定义保留在原来的位置,只需在文件顶部附近添加一个声明即可。
无论哪种方式,当您将
feCallback
传递给FSEventStreamCreate
时,编译器都会知道它是什么。The
&
is the “address of” operator, and evaluates to a pointer to its operand. It's not really necessary here; a function name not in a function call always evaluates to the pointer to the function, with or without&
.So this passes the pointer to the
feCallback
function, which is how the FSEventStream object will call it back.That does make sense, but no, that's not correct. They are passing the function itself.
It is possible to declare a variable that holds a pointer to a function. If
feCallback
were such a variable, thenfeCallback
and&feCallback
would mean different things:feCallback
would be the pointer to the function (inside thefeCallback
variable), whereas&feCallback
would be the pointer to the variable.In your case, though, with
feCallback
being a function, the two expressions are equivalent; either way passes the function pointer.You haven't declared that identifier (in this case, that function).
That's the problem. You have the callback function defined after your use of its name, but you did not declare the function before using its name. You must declare the function before you can use it (or pass it anywhere).
The reason is because the compiler doesn't know what “
feCallback
” is until you tell it, which is what the declaration does. When you try to refer to “feCallback
” before declaring or defining it, the compiler doesn't know what you're talking about.The other way would be to move the function's definition up. A definition counts as a declaration for everything that follows it. I'd leave the definition where it is, though, and just add a declaration near the top of the file.
Either way, then the compiler will know what
feCallback
is when you pass it toFSEventStreamCreate
.