2016-10-19 46 views
3

中没有类型命名'type'我知道这种类型的问题已经被问到,但我无法弄清我得到的错误的原因..错误:在'class std :: result_of

我想测试锁实现以下使用多线程给出:

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

体型均匀初始化语法在传统之一:

TTAS t{}; // no ambiguity here 

还有一个问题与您的代码test2的价值需要它的参数,但TTAS包含std::atomic<bool>既不是可复制也不可移动。

虽然这是有点用词不当,因为它不是所有的统一

+0

感谢您的快速响应..我将'原子'改为'原子 *'修复了这个问题.. – anirudh

+1

@anirudh我的第一步会让'test2'接受一个参考,但我不会不知道你的用例,所以你的解决方案可能是完全有效的,最好的。 – krzaq

3

TTAS t();是一个函数声明。如果你想要一个默认构造的变量,则声明它为TTAS t;

相关问题