2017-01-05 33 views
3

C++のスレッドを理解しようとしていますが、この問題を解決する方法がわかりません。スレッドエラー:非静的メンバー関数の無効な使用

私は、「作成」が、私はこのエラーを取得すると呼ばれる機能を実行するために2つのスレッドを呼び出したい:

error: invalid use of non-static member function

私はこのトピックについての他の質問を読んだが、私は実際に作る方法を理解していません私のコードが動作します。

私が間違っていることを誰かに説明して解決策を見つけるのを手伝ってもらえますか?

test_class.cpp

void test_class::generateS(){ 

    map1=new multimap<double,vector<int>>; 
    map2=new multimap<double,vector<int>>; 

    thread thread_1(createS, 0, nCells/2, map1); 
    thread thread_2(createS, nCells/2, nCells, map2); 

    thread_1.join(); 
    thread_2.join(); 
} 

void test_class::createS(int startP, int endP, Costs *mapPointer){ 
    //i do some stuff 
} 

test_class.h

void createS(int start, int end, Costs *mapPointer); 
void generateS(); 
+0

CreateS()クラスを静的にしてみてください。 –

答えて

1
thread thread_1(&test_class::createS, this, 0, nCells/2, map1); 
thread thread_2(&test_class::createS, this, nCells/2, nCells, map2); 

注:createSは、オブジェクトの状態に依存しない場合は、良いことstaticクラスのメンバにすると、あなたのやり方を呼んでください。

関連する問題