0

我正在尝试传递一个将函数存储为指定初始化程序的聚合,但没有成功。

这是我尝试过的:

    typedef struct Handlers
    {
        std::function<void()> func1 = nullptr;
        std::function<void()> func2 = nullptr;
        std::function<void()> func3 = nullptr;
        std::function<void()> func4 = nullptr; 
    } Handlers;

    class Person
    {
    public:
        Person(const char *name, 
               uint16_t age, 
               Handlers &&handlers);

        void doSomething();
    };

这就是我试图传递函数的方式

int main(int argc, char *argv[])
{
   Person p1(
        "Guy",
        28,
        { 
          .func1 = []() {},
          .func2 = []() {},
          .func3 = []() {},
          .func4 = []() {}
        });
}

错误消息之一:

no known conversion for argument 3 from ‘&lt;brace-enclosed initializer list>’ to ‘Handlers&&’
   35 |     Person(const char *name, uint16_t age, Handlers &&handlers);
4

0 回答 0