我尝试在我的项目中使用委托(delegate),但在编码越来越深的过程中,我遇到了一些奇怪的行为。
这里显示我的问题的最大简化代码:
template<typename T>
class Slave{
public:
typedef void (*F)(T option);
Slave<T>::F f;
Slave(Slave<T>::F *f){
this->f = f;
}
};
template<typename T>
class Master {
public:
T option;
Slave<T>* slave;
Master(T option, Slave<T>* slave){
this->option = option;
this->slave = slave;
}
void blink(){
slave->f(option);
}
};
void loop(){
Master<int> *m;
m = new Master<int>(3, new Slave<int>([](int option) -> void {
//blink option times
})); // Here error: no matching function for call to 'Slave<int>::Slave(Core::Core()::<lambda(int)>)'
m->blink();
};
最佳答案
问题在于 Slave<T>::F
已经是一个指针。
Slave(Slave<T>::F *f){
this->f = f;
}
您的参数是指向函数指针的指针。只需将其更改为:
Slave(Slave<T>::F f){
this->f = f;
}
关于c++ - 在继承类中没有匹配的泛型委托(delegate)作为 lambda 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56485332/