2017-02-14 6 views
-1
ことができない引数とねじ関数を呼び出します

私はよく質問しています.1つ以上の引数を持つスレッドとしてC++関数を呼び出すには、ここや複数のチュートリアルで数回答えられました。これはこれを行う方法です:私はこの質問を知って<p><strong>はすでにものを答えのように表示されますが、それらに与えられた答えは私のために動作しないので、私は</strong></p> それらのdublicateになるこの質問を考えていない

(例:this questionから直接取得)

01251641私はこのコードおよびこれを行う方法のより多くの(多かれ少なかれ同じ)の例の両方をcopypasting試みた、まだ、私はそのような g++ test.cpp -o test.appとしてtermial介して(コンパイルするたびに(.APPのため追加しなければならないが
#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(); 
} 

私は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引数に螺関数を作ることができ、すべての人に比べて間違っている、と私は以来しています、私はこの質問に私はdoesnのスレッドを使用して、私の知る限りでは、引数

とねじ関数を呼び出すにはどうすればよい多くの」の複製を考えていない、同様の問題をexperiancing人々によって尋ね質問を発見していません特定のコンパイラフラグを必要とします。そして、私が完全に細かいことは、引数のないスレッド関数でプログラムを実行できるので、自分のコンピュータやコンパイラがスレッドを完全に使用できないと主張することはできません。

+0

'g ++ --version'を実行すると、何が報告されますか? C++ 11( 'std:thread'が追加された標準)を完全にサポートしていない非常に古いバージョンのClang(macOSの' gcc'と 'g ++'は通常Clangコンパイラのエイリアスです)を持っていますか? –

+0

スレッドのprocを 'char const *'に変更してください –

+3

コンパイラスイッチを追加する-std = C++ 11。 –

答えて

1

gccのバージョンによっては、-std = C++ 11、-std = C++ 0xというコンパイラスイッチを追加する必要があります。

1

は、私はC++ 14でそれhere

をコンパイルすることができていますし、以下のように出力を得ました。

task1 says: Hello 

-std=c++11以上のフラグを使用してコンパイルします。

関連する問題

 関連する問題