2016-05-22 7 views
2

クラス内に4つのスレッドを作成して、それぞれ別のメンバー関数を使用して各ベクトルの内容を出力する際に​​問題があります。しかし、スレッドを作成すると、私はこれらの4行にエラーno instance of constructor "std::thread::thread" matches the argument listを得ています。私は、なぜスレッドが別のメンバ関数を使用しようとしている場合にはうまくいかないのか分かりません。彼らがクラスの中にいるからかもしれませんか?これらの4つのエラーをどのように修正すればよいでしょうか?クラス内で4つのスレッドを作成する方法C++

class PrintfourVectors 
{ 
private: 
    vector<string> one; 
    vector<string> two; 
    vector<string> three; 
    vector<string> four; 
public: 
    void printOne() 
    { 
     // do stuff 
    } 

    void printTwo() 
    { 
     // do stuff 
    } 


    void printThree() 
    { 
     // do stuff 
    } 

    void printFour() 
    { 
     // do stuff 
    } 

    void makeFourThreads() 
    { 
     thread threadone(printOne); // error here 
     thread threadtwo(printTwo); // error here 
     thread threadthree(printThree); // error here 
     thread threadfour(printFour); // error here 

     threadone.join(); 
     threadtwo.join(); 
     threadthree.join(); 
     threadfour.join(); 

    } 

}; 

答えて

2

一つの問題は、あなたが非静的メンバ関数を呼び出している、そしてそれらが機能にthisポインタになり、「隠れた」最初の引数を持っているということです。したがって、非静的メンバ関数を使用してスレッドを作成する場合は、オブジェクトインスタンスをスレッド関数の引数として渡す必要があります。

thread threadone(&PrintfourVectors::printOne, this); 
//           ^^^^ 
// Pass pointer to object instance as argument to the thread function 
同様

関連する問題