gpt4 book ai didi

c++ - std::atomic 错误:没有为后缀 ‘operator++(int)’ 声明 ‘++’ [-fpermissive]

转载 作者:太空狗 更新时间:2023-10-29 20:34:26 26 4
gpt4 key购买 nike

我试图通过不同的线程更新一个 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++ 中收到以下错误。

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

最佳答案

fn_write() 被声明为一个const 成员函数,其中done 数据成员不能被修改。

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

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

或者,你可以让done成为mutable:

mutable std::atomic<int> done;

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

关于c++ - std::atomic 错误:没有为后缀 ‘operator++(int)’ 声明 ‘++’ [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48533476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com