大家好,能帮本人看下,下面的代码吗?想不通哪里出错了。先谢了啦。
#include <iostream> #include <functional> using namespace std; class Node{ public: explicit Node(int i = 300){ cout << "Node Constructor has called!" << endl; } private: int _i, _j, _k; }; typedef function<void(Node&, long, long)> Action; struct Command{ Action action; UINT msg; }; class CommandEXE{ public: CommandEXE(long wParam, long lParam) : _wParam(wParam) , _lParam(lParam){ cout << "CommandEXE called with wParam, lParam" << endl; } private: long _wParam; long _lParam; }; Action DerivedAction(CommandEXE cmdexe){ return [=] (Node& node, long wParam, long lParam){ cmdexe(node, wParam, lParam); }; } int main(){ Action a = DerivedAction(CommandEXE(40, 50)); return 0; }
然后,错误信息是这样的:D:\WinPro\LamdaTaste\main.cpp|82|error: no match for call to “(const CommandEXE) (Node&, long int&, long int&)”|
解决方案
40
眼拙没看到 operator() 的声明或定义,那么所谓仿函数是在哪里呢?
120
cmdexe(node, wParam, lParam); 这里调用的不是构造函数,你对象都已经构造好了,用的是opertor()