Question

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?

Was it helpful?

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.

OTHER TIPS

In-class initialization is only allowed for static const integral types in C++03, and your variable isn't static.

Either make it static (in this case, you should - note that you also have to define it in this casee), or use a C++11 compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top