I know this question appear similar to already answered ones, but since the answer given to them does not work for me, i do not regard this question to be a dublicate of them
I am well aware that the question: how do i call a c++ function as a thread which has 1 or more arguments has been answered several times -- both here, and on various tutorials -- and in every case the answer is simply that this is the way to do it:
(example taken directly from 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();
}
However i have tried copypasting both this code and many more (more or less identical) examples of how to do this, and yet, every time i compile (through termial as such g++ test.cpp -o test.app
(the .app must be added because i am on a Mac (note that this way of compiling does in fact work for me, and that the error is simply not a result of me not knowing how to compile a c++ program))) such a program i get this error message:
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) {}
My question therefor is, what am i doing wrong compared to all the people who possitively can make threaded functions with arguments, and since i have not found any questions asked by people experiancing similar problems, I do not regard this question a duplicate of the many How do i call a threaded function with arguments
As far as i know, using threads doesn't require any specific compiler flags, and since i perfectly fine can run programs with threaded functions without arguments, you can not claim that my computer or compiler is incabable of using threads altogether.
Depending on gcc version, you should add the compiler switch -std=c++11, or -std=c++0x.