Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用std::shared_ptr和std::thread时出现编译器错误_C++_C++11 - Fatal编程技术网

C++ 使用std::shared_ptr和std::thread时出现编译器错误

C++ 使用std::shared_ptr和std::thread时出现编译器错误,c++,c++11,C++,C++11,我正在尝试使用类测试中的共享\u ptr启动线程,但我收到以下错误: /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2:注意:参数1从'std::shared_ptr'到'std::shared_ptr&'的转换未知。 示例代码: std::shared_ptr<Test> test = std::make_shared<Test>(); std::thread t

我正在尝试使用类
测试中的
共享\u ptr
启动线程,但我收到以下错误:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2:注意:参数1从'std::shared_ptr'到'std::shared_ptr&'的转换未知。

示例代码:

    std::shared_ptr<Test> test = std::make_shared<Test>();
    std::thread th(&Test::run, test); // Compiler error


    Test* test2 = new Test;
    std::thread th(&Test::run, test2); // okay
std::shared_ptr test=std::make_shared();
std::thread th(&Test::run,Test);//编译错误
Test*test2=新测试;
std::thread th(&Test::run,test2);//可以

注意:第一个示例中的VS2013在windows中运行良好。

这看起来像是您正在使用的gcc版本中的一个bug,因为它应该可以工作。看着它确实有用

作为解决方法,您可以尝试

std::shared_ptr<Test> test = std::make_shared<Test>();
std::thread th(std::bind(&Test::run, test))
std::shared_ptr test=std::make_shared();
std::thread th(std::bind(&Test::run,Test))

这看起来像是您正在使用的gcc版本中的一个bug。
std::thread th(std::bind(&Test::run,Test))
是否工作得更好(可能不会,因为它们可能使用一些常见的内部代码)…std::bind工作得很好!谢谢但它确实能起作用。