我寫了一個Event
類作爲回調函數的包裝,實現爲std::function
s。這是什麼樣子:C++ std ::函數作爲函子的參數
class Event
{
public:
Event() : default_handler([]() {});
Event(const std::function<void()> handler) : default_handler(handler);
void SetHandler(std::function<void()> handler)
{
custom_handler = handler;
}
void operator()(void)
{
default_handler();
custom_handler();
}
private:
const std::function<void()> default_handler;
std::function<void()> custom_handler;
};
然後,另一個類中,我有一個事件的一個實例:
class Control
{
public:
Control();
//Should call constructor Event()
Event myEvent1;
//Should call constructor Event(std::function<void()>)
Event myEvent2([]() {/*do stuff... */})
};
然而,這不會對VC++編譯,生成錯誤C3646(未知覆蓋說明符)和錯誤C4430(缺少類型說明符 - int假定),這兩個處理程序的myEvent2語法錯誤更多。我哪裏做錯了?
將來,發佈實際的錯誤。儘管你可能不瞭解他們,但他們實際上有有用的信息! – Yakk