c++ - 没有构造函数 "std::thread::thread"的实例与参数列表匹配

标签 c++

<分区>

Error (active) E0289 no instance of constructor "std::thread::thread" matches the argument list

#ifndef TIMER_H
#define TIMER_H

#include <thread>
#include <chrono>

class Timer
{
std::thread Thread;
bool Alive = false;
long CallNumber = -1L;
long repeat_count = -1L;
std::chrono::milliseconds interval = std::chrono::milliseconds(0);
std::function< void(void) > funct = nullptr;

void SleepAndRun()
{
    std::this_thread::sleep_for(interval);
    if (Alive)
         Function()();
}

void ThreadFunc()
{
    if (CallNumber == Infinite)
        while (Alive)
            SleepAndRun();
    else
        while (repeat_count--)
            SleepAndRun();
}

public:
    static const long Infinite = -1L;

    Timer(){}

    Timer(const std::function<void(void)> &f) : funct (f) {}

    Timer(const std::function<void(void)> &f,
        const unsigned long &i,
        const long repeat = Timer::Infinite) : funct(f),

    interval(std::chrono::milliseconds(i)),
                                               CallNumber(repeat) {}

    void Start(bool Async = true)
    {
        if (isAlive())
            return;
        Alive = true;
        repeat_count = CallNumber;
        if (Async)
            Thread = std::thread(ThreadFunc, this);//  <- There is an error
        else
            this->ThreadFunc();
    }
    void Stop()
    {
        Alive = false;
        Thread.join();
    }
    void SetFunction(const std::function<void()> &f)
    {
        funct = f;
    }

    bool isAlive() const { return Alive; }

    void RepeatCount(const long r)
    {
        if (Alive)
            return;
        CallNumber = r;
    }
    long GetLeftCount() const { return repeat_count; }

    long RepeatCount() const { return CallNumber; }

    void SetInterval(const unsigned long &i)
    {
        if (Alive)
            return;
        interval = std::chrono::milliseconds(i);
    }
    unsigned long Interval() const { return interval.count(); }

    const std::function<void(void)> &Function() const
    {
        return funct;
    }
};

#endif // !TIMER_H

Severity Code Description Project File Line Suppression State Error C3867 'Timer::ThreadFunc': non-standard syntax; use '&' to create a pointer to member 53

第53行错误

谁能解释一下这个错误是什么意思?

最佳答案

ThreadFunc 是非静态成员函数,它需要 this 指针。

最简单的修复方法是在 lambda 中传递:

Thread = std::thread([this] { this->ThreadFunc(); });

或者正如@Some programmer dude 指出的那样:

Thread = std::thread(&Timer::ThreadFunc, this);

关于c++ - 没有构造函数 "std::thread::thread"的实例与参数列表匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49512288/

相关文章:

c++ - 无法在新安装的 Visual Studio Code 上运行 C++ 代码

c++ - (Ubuntu 14.04) apt-get libopencv-dev,但出现错误 : Unable to correct problems, 你持有损坏的包

c++ - 使用 std::enable_if 作为函数参数与模板参数有什么区别?

c++ - 根据对象的动态类型从一组重载中调用函数

c++ - Qt 5 : emit signal from non-Qt thread

c++ - 头文件中的 QList 声明导致源文件中出现段错误

c++ - 如何使用 operator new 创建动态二维字符数组?

c++ - 具有多种键类型的关联数组,可能吗?

c++ - 如何强制 ld 使用静态库而不是共享库?

c++ - Curiously Recurring Template Pattern (CRTP) 是正确的解决方案吗?