错误:'无法专门化功能模板'C2893'std :: invoke'

时间:2016-05-19 17:00:37

标签: c++ visual-studio-2013 mfc

我正在Visual Studio 2013中编写MFC程序,我不断收到以下两个错误

Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'

Error C2672 'std::invoke': no matching overloaded function found

错误与文件xthread第238行

有关

我在c ++ / MFC上相当新,我正在尝试编写一个在后台运行到系统时间的函数。

这是我正在使用的代码:

void task1(ExperimentTab& dlg)
{
    while (true)
    {
        CString showtime = CTime::GetCurrentTime().Format("%H:%M:%S");
        int x = dlg.m_showTime.GetWindowTextLengthA();
        dlg.m_showTime.SetWindowTextA(_T(""));
        dlg.m_showTime.ReplaceSel(showtime, 0);
    }
}

void mainThread()
{
    std::thread t1(task1);
    t1.join();
}

然后按下按钮启动时间,但同样的按钮也用于停止时间。

1 个答案:

答案 0 :(得分:5)

函数task1采用单个参数(用作线程体),但在t1构造函数中没有传递。 编译器无法在没有方法参数的情况下创建std::invoke调用task1函数。

修复它的调用构造函数:std::thread t1(task1, std::ref(dlg));,其中dlgExperimentTabstd::ref确保dlg将通过引用传递给线程。

BTW:从其他线程更新MFC组件可能会导致一些数据争用。此外 - while(true)线程将通过具有第二分辨率的每秒定时器的多次更新消耗100%的CPU。

相关问题