c++c++11constantsstdatomic

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


I am trying to update an atomic variable through different threads and getting this error. This is my code.

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)) ;
    }
}

I am getting the following error in line 8 done++.

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


Solution

  • fn_write() is declared as a const member function, inside which the done data member can't be modified.

    Depending on your intent, you can make fn_write() be non-const:

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

    Or, you can make done be mutable:

    mutable std::atomic<int> done;
    
    bool fn_write (int size) const {
        ... ...
    }