Search code examples
c++classstaticinitializationmember

Initialize a static const class member with no parameters in constructor of a custom class in C++


How can I initialize a static const class member of a custom class in C++?

Here is what I tried so far:

Header file:

class A
{
private:
static const B b;
};

Source file:

const B A::b;

Class`s B constructor has no parameters.

The approach does not work. The b becomes red underlined in the source file and it is written over there const member "A::b" requires an initializer.


Solution

  • Did you provide a default constructor for B?

    class B
    {
    public:
        B() {}
    };
    

    It worked here