Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top