trafalgarLaww
trafalgarLaww

Reputation: 515

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.

Upvotes: 1

Views: 136

Answers (1)

rflobao
rflobao

Reputation: 597

Did you provide a default constructor for B?

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

It worked here

Upvotes: 2

Related Questions