调用带参数不可能
问题描述:
我知道这个问题出现类似已经回答的,但自从给他们回答不工作对我来说,我不认为这个问题是其中的一个dublicate调用带参数不可能
螺纹功能我很清楚,我该如何调用C++函数作为一个具有一个或多个参数的线程已被多次回答 - 无论是在这里还是在各种教程中 - 并且在任何情况下答案都是简单的这是做它的方式:
(示例直接从this question)
#include <string>
#include <iostream>
#include <thread>
using namespace std;
// The function we want to execute on the new thread.
void task1(string msg)
{
cout << "task1 says: " << msg;
}
int main()
{
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
// Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
但是我已经尝试copypasting这两个代码和更多(或多或少相同)如何做到这一点的例子,但是,每次我编译(通过这样的例子g++ test.cpp -o test.app
(必须添加.app我在Mac上(请注意,这种编译方式实际上对我很有用,而且错误不是我不知道如何编译C++程序)))这样的程序我得到这个错误消息:
test.cpp:16:12: error: no matching constructor for initialization of 'std::__1::thread'
thread t1(task1, "Hello");
^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:389:9: note: candidate constructor template not viable: requires single argument '__f', but
2 arguments were provided
thread::thread(_Fp __f)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:297:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
thread(const thread&);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:304:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
thread() _NOEXCEPT : __t_(0) {}
我的问题就是为此,我在做什么错相比,所有谁possitively可以使线程功能与参数的人,因为我还没有发现人experiancing类似的问题问任何问题,我不认为对这个问题的很多下如何调用带参数的
螺纹功能据我所知的副本,使用线程没有按” t需要任何特定的编译器标志,因为我完全可以使用没有参数的线程函数运行程序,所以不能声称我的计算机或编译器完全可以使用线程。
答
根据gcc版本,您应该添加编译器开关-std = C++ 11或-std = C++ 0x。
如果你做'g ++ --version'它报告什么?你有没有完全支持C++ 11(添加'std:thread'的标准)的Clang的非常旧的版本(在macOS'gcc'和'g ++'通常是Clang编译器的别名)? –
更改线程处理程序采取'char const *' –
添加编译器开关-std = C++ 11。 –