c++codepad

ISO C++ forbids initialisation of private const unsigned long?


I'm using codepad.org

class a {
    private:
        const unsigned long b = 100;
};

Line 3: error: ISO C++ forbids initialization of member 'b'

compilation terminated due to -Wfatal-errors.

Sorry if I've missed something obvious, but what could be wrong?


Solution

  • Before C++11, you can only initialise a static const data member in the class definition:

    static const unsigned long b = 100;
    

    This is usually what you want anyway. It doesn't make much sense to have all instances of a have this constant value duplicated among them.

    If you enable C++11 support, your code will compile.