c++ - 在继承类中没有匹配的泛型委托(delegate)作为 lambda 参数

标签 c++ c++11 arduino

我尝试在我的项目中使用委托(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/

相关文章:

c++ - 无法将 '<brace-enclosed initializer list>' 转换为 'double' 作为返回

C 检查字符串的最后一个字符是否与 X 相等

c++ - 数字系统转换器不适用于特定编译器 (Dev-C++)

c++ - 什么排序方法使用 : quicksort, bucket sort, radix, ... 对于微小的数据对? (c++)

c++ - 在cpp中动态更改属性值

c++ - 带字符偏移量 0x01F 的 Base 256 GUID 字符串表示

c++ - Arduino编译错误: xxx does not name a type (despite it being declared 10 lines before)

c++11 - 什么是函数类型的右值引用?

c++ - DMA 的同步要求

c++ - 我可以安全地将指向 const 成员的指针转换为相同类型但非常量吗?