繁体   English   中英

std :: atomic error:没有'operator ++(int)'声明为postfix'++'[-fpermissive]

[英]std::atomic error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]

我试图通过不同的线程更新atomic变量并获得此错误。 这是我的代码。

class counter {
    public:
    std::atomic<int> done;

    bool fn_write (int size) const {
        static int count = 0;
        if (count == size) {
            done++;
            count = 0;
            return false;
        } else {
            count++;
            return true;
        }
    }
};

int main() {
    counter c1;
    for (int i=0; i<50; i++) {
        while (! c1.fn_write(10)) ;
    }
}

我在第8行done++得到以下错误。

错误:没有'operator ++(int)'声明为postfix'++'[-fpermissive]

fn_write()被声明为const成员函数,在其中无法修改done数据成员。

根据您的意图,您可以使fn_write()为非const:

bool fn_write (int size) {
    ... ...
}

或者,你可以done mutable

mutable std::atomic<int> done;

bool fn_write (int size) const {
    ... ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM