簡體   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