简体   繁体   English

C ++将函数指针作为参数传递

[英]C++ passing function pointers as arguments

I'm trying to pass a function pointer, but I keep getting a compiler error. 我正在尝试传递函数指针,但我不断收到编译器错误。 I don't normally pass function pointers, but this case demands it. 我通常不会传递函数指针,但这种情况需要它。 I think that you need only see the declarations to see what I'm doing wrong. 我认为你只需看到声明就可以看出我做错了什么。

in the header file I have: 在头文件中我有:

pthread_t * createThread(void *(*func)(void *), string arg)

and in the implementation it is the same: 在实施中它是相同的:

pthread_t * createThread(void *(*func)(void *), string arg)

In the call to this function I'm doing: createThread(&afunction, "ran again") 在调用这个函数时我正在做:createThread(&function,“再次运行”)

And the declaration of afunction is: 并且声明的功能是:

void *afunction(void *ptr) //(no header, same for both declaration and implementation).

but the compiler spits this out: 但编译器吐了出来:

    Undefined symbols for architecture x86_64:
  "createThread(void* (*)(void*), std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
      spawnSingleThreadTest()     in threadTests.o

You didn't link the implementation to your executable. 您没有将实现链接到可执行文件。 It's not about function pointer. 这不是关于函数指针。 It's how you compiled your source. 这是你编译源代码的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM