简体   繁体   中英

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")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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