我遇到了一个c++模板函数poniter问题,请大家帮忙。非常感谢。
class AAA {
public:
template<typename K>
void dooo(K str) {
std::cout << str << std::endl;
}
};
template<typename Class, typename ret, typename K>
using Func = ret (Class::*) (K);
int main() {
Func<AAA, void, int> myFunc = &AAA::dooo;
myFunc(3);
return 0;
}
编译:clang++ -std=c++11 -o c c.cc
错误:
error: called object type 'Func<AAA, void, int>' (aka 'void (AAA::*)(int)') is not a function or function pointer
myFunc(3);
~~~~~~^
1 error generated.
最佳答案
由于dooo()
不是AAA
的static
成员函数,因此您需要一个AAA
的实例来在以下位置调用 myFunc
:
AAA a;
(a.*myFunc)(3);
关于C++模板函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51392509/