在类中使用std :: thread时出错

wanglinski

我尝试在Visual Studio 2013中使用std :: thread。它可以像这样正常工作。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for
#include <vector>
void thread_task(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "hello thread "
        << std::this_thread::get_id()
        << " paused " << n << " seconds" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::thread> threads;
    std::cout << "Spawning 5 threads...\n";
    for (int i = 0; i < 5; i++) {
        threads.emplace_back( std::thread(thread_task, i + 1));
    }
    std::cout << "Done spawning threads! Now wait for them to join\n";
    for (auto& t : threads) {
        t.join();
    }
    std::cout << "All threads joined.\n";
    return 0;
}

但是,当我尝试在Class中创建线程时,遇到了一些编译错误。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for
#include <vector>
class MyClass
{
public:
    MyClass();
    void thread_task(int n);
    ~MyClass();
private:
};
MyClass::MyClass()
{
    std::vector<std::thread> threads;
    std::cout << "Spawning 5 threads...\n";
    for (int i = 0; i < 5; i++) {
        threads.emplace_back(std::thread(&MyClass::thread_task, i + 1));
    }
    std::cout << "Done spawning threads! Now wait for them to join\n";
    for (auto& t : threads) {
        t.join();
    }
    std::cout << "All threads joined.\n";
}
void MyClass::thread_task(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "hello thread "
        << std::this_thread::get_id()
        << " paused " << n << " seconds" << std::endl;
}
MyClass::~MyClass(){}
int _tmain(int argc, _TCHAR* argv[])
{   
    MyClass test();
    return 0;
}

错误消息将复制到下面。

1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2064: term does not evaluate to a function taking 1 arguments
1>          class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::_Do_call<,0>(std::tuple<>,std::_Arg_idx<0>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::_Do_call<,0>(std::tuple<>,std::_Arg_idx<0>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::operator ()<>(void)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::operator ()<>(void)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(192) : while compiling class template member function 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)'
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(187) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(205) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\thread(49) : see reference to function template instantiation 'void std::_Launch<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>>(_Thrd_t *,_Target &&)' being compiled
1>          with
1>          [
1>              _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1>          ]
1>          d:\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(29) : see reference to function template instantiation 'std::thread::thread<void(__thiscall MyClass::* )(int),int>(_Fn &&,int &&)' being compiled
1>          with
1>          [
1>              _Fn=void (__thiscall MyClass::* )(int)
1>          ]
1>

是什么原因引起的问题?

感谢@malchemist,在添加此功能时可以解决该问题。但这让我感到困惑,即使是像答案所示那样编写,这段代码错误仍然会有一些编译错误。

谁能帮我?非常感谢。

恶意化学家

您必须指定一个对象才能在新线程内调用函数。试试这个:

threads.emplace_back(std::thread(&MyClass::thread_task,this, i + 1));

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Asynctask中使用Thread.sleep时出错

来自分类Dev

使用Thread.sleep()时run()方法出错

来自分类Dev

在项目中使用Bitmap类时出错

来自分类Dev

在swift 5.1类中使用属性包装器时出错

来自分类Dev

在 tkinter 中使用类时出错?(面向对象)

来自分类Dev

使用std :: map :: at时出错

来自分类Dev

在std :: thread中使用std :: vector

来自分类Dev

使用Spinner类时出错

来自分类Dev

使用 python 类时出错

来自分类Dev

在宏中使用“#”时出错

来自分类Dev

在C ++模板中嵌套的类中使用基类的成员时出错

来自分类Dev

扩展类时出错

来自分类Dev

扩展类时出错

来自分类Dev

在函数方法中使用std :: thread

来自分类Dev

使用类成员函数作为比较器调用std :: equal时出错

来自分类Dev

在视觉cpp控制台应用程序中使用std :: cout时出错

来自分类Dev

使用std :: string :: npos时出错

来自分类Dev

在模板类中使用r和l值构造函数时出错

来自分类Dev

尝试在Spring MVC中使用Maven从WSDL生成类时出错

来自分类Dev

在 C# 中使用 CsvHelper 编写类列表时输出错误

来自分类Dev

使用类然后调用该类时出错

来自分类Dev

使用CupSodaSimulator类os PySB时出错

来自分类Dev

使用junit设置类路径时出错

来自分类Dev

使用 AutoSizing 膨胀类 TextView 时出错

来自分类Dev

使用 WindowSetup 类变量时出错

来自分类Dev

尝试在Linq中使用包含时出错

来自分类Dev

在Python中使用Sapi Voice时出错

来自分类Dev

在Delphi中使用Power时出错

来自分类Dev

在Rails中使用Bootstrap Sass时出错