加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 错误:’class std :: result_of中没有名为’type’的类型

发布时间:2020-12-16 10:04:17 所属栏目:百科 来源:网络整理
导读:我知道这类问题已被提出,但我无法弄清楚我得到错误的原因.. 我正在尝试使用多个线程测试下面给出的锁实现: class TTAS{ atomicbool state;public: TTAS(){ state = ATOMIC_FLAG_INIT; } void lock() { while(true) { while(state) {}; // using exchange()
我知道这类问题已被提出,但我无法弄清楚我得到错误的原因..

我正在尝试使用多个线程测试下面给出的锁实现:

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...>)
         ^

有人可以解释这个错误.提前致谢

解决方法

看来你遇到了 most vexing parse

TTAS t();

t是一个不带参数的函数并返回TTAS.

优先考虑遗留的一致的初始化1语法:

TTAS t{}; // no ambiguity here

您的代码还有一个问题,test2按值获取其参数,但TTAS包含std::atomic<bool>,它既不可复制也不可移动.

1虽然这有点用词不当,因为它不是那么统一

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读