C++将函数指针作为参数传递
C++ passing function pointers as arguments
我正在尝试传递函数指针,但不断收到编译器错误。我通常不传递函数指针,但这种情况需要它。我认为你只需要看到声明就知道我做错了什么。
在头文件中,我有:
pthread_t * createThread(void *(*func)(void *), string arg)
在实现中是相同的:
pthread_t * createThread(void *(*func)(void *), string arg)
在调用这个函数时,我正在做:createThread(&afunction, "ran Again")
函数的声明是:
void *afunction(void *ptr) //(no header, same for both declaration and implementation).
但是编译器吐出了这个:
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
您没有将实现链接到可执行文件。这与函数指针无关。这就是你编译源代码的方式。
相关文章:
- 使用指向成员的指针将成员函数作为参数传递
- 修改函数中的指针(将另一个指针作为参数传递)
- 将成员函数指针作为参数传递给模板方法
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将函数指针数组中的函数指针作为模板参数传递
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 如何将指针变量作为引用参数传递?
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- Qt5 - 如何将"QList<T> *"指针作为信号参数传递,并在完成后将其删除?
- C++将指向成员的指针作为模板参数传递
- 通过引用函数传递指针参数是什么意思?
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- 将函数指针作为函数参数传递的目的
- 将派生类作为参数传递给方法,该方法是具有智能指针的基类
- 使用 C++11 将指向成员函数的指针作为参数传递
- 如何在程序集函数中将元素数组作为参数传递时转发 ARM 寄存器的地址指针
- 在一行中将默认类型值上的指针作为参数传递
- 将双指针作为参数传递给需要引用 std::vector <double>的函数