[英]no match for 'operator<<' in 'std::cout << ::operator++(int)(0)'
[英]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.