5

使用此代码,我得到了错误:

错误 1 ​​错误 C2064:术语不计算为采用 1 个参数的函数 c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 Pipeline

class PipelineJob {
private:
    std::thread *thread;
    void execute(PipelineJob* object);
public:

    void execute(PipelineJob* object)
    {
    }

    PipelineJob()
    {
        this->thread = new std::thread(&PipelineJob::execute, this);
    }
};

我尝试了很多变化,现在有人如何解决这个问题?

4

1 回答 1

17

为简单起见删除模板和指针,这或多或少是您想要的:

class PipelineJob 
{
private:
    std::thread thread_;
    void execute(PipelineJob* object) { ..... }
public:
    PipelineJob()
    {
      thread_ = std::thread(&PipelineJob::execute, this, this);
    }
    ~PipelineJob() { thread_.join(); }
};

请注意,this向构造函数传递了两次std::thread:一次为成员函数的隐式第一个参数,第二次为PipelineJob* object成员函数的可见参数。

如果您的execute成员函数不需要外部PipelineJob指针,那么您将需要类似

class PipelineJob 
{
private:
    std::thread thread_;
    void execute() { ..... }
public:
    PipelineJob()
    {
      thread_ = std::thread(&PipelineJob::execute, this);
    }
    ~PipelineJob() { thread_.join(); }
};
于 2013-05-23T16:26:04.080 回答