2016-10-19 4 views
3

このタイプの質問は既に尋ねられていますが、私はエラーの理由を理解できません。エラー:クラス 'std :: result_of'にタイプ 'type'がありません

私は複数のスレッドを使用して、以下に示すように、ロックの実装をテストしようとしている:

class TTAS 
{ 
    atomic<bool> state; 
public: 
    TTAS(){ 
     state = ATOMIC_FLAG_INIT; 
    } 
    void lock() { 
     while(true) { 
      while(state) {}; 
      // using exchange() which is equivalent to getAndSet but with lookup 
      if (!state.exchange(true)) { 
       return; 
      } 
     } 
    } 
    void unlock() { 
     state.exchange(false); 
    } 
}; 

私は以下のコード使用して、このためのスレッドを作成しています:私はこのコードをコンパイルすると

void test2(TTAS t) { 

} 
TTAS t(); 

for (int i = 0; i < 10; i++) { 

    // Create a thread and push it into the thread list and call the distributedWrite function 
    threadList.push_back(std::thread(test2, std::ref(t))); 
} 

を私は以下のエラーを取得する。私はここで間違っていることを知りません。

In file included from /usr/include/c++/5/thread:39:0, 
       from assgn4.cpp:17: 
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’: 
/usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(TTAS); _Args = {std::reference_wrapper<TTAS()>}]’ 
assgn4.cpp:186:60: required from here 
/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’ 
     typedef typename result_of<_Callable(_Args...)>::type result_type; 
                  ^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’ 
     _M_invoke(_Index_tuple<_Indices...>) 
     ^

誰かがこのエラーを説明できますか?事前

答えて

4

のおかげであなたがmost vexing parse

TTAS t(); 

tが関数は引数を取らないとTTASを返すで遭遇したようです。

レガシー1に渡って均一な初期化構文好む:あなたのコードtest2との1つの以上の問題があります

TTAS t{}; // no ambiguity here 

をコピー可能でも移動可能でもないその値によってその引数を取りますが、TTASstd::atomic<bool>が含まれています。

それはすべて均一な

+0

私は 'atomic 'を 'atomic *'に変更しました。問題を修正しました。 – anirudh

+1

@anirudh私の最初のステップは、 'test2'がリファレンスを受け入れるようにしていましたが、あなたのユースケースを知っているので、あなたのソリューションは完全に有効で、最高のものになるでしょう。 – krzaq

3

TTAS t();ないように、これは、幾分誤った名称であるが、関数宣言です。デフォルトで構築された変数を使用する場合は、TTAS t;と宣言してください。

関連する問題