Damian
Damian

Reputation: 308

C++ No matching constructor when using inheritance

I have code like this:

class A{
public:
    int b;
};

int main()
{
    A a{.b = 5};
}

And the program compiles.

However, when I add another class and I make A inherit that class (like below), it throws an error "no matching function for call to ‘A::A()’" (or "No matching constructor for initialization of A").

class C{

};

class A: public C{
public:
    int b;
};

int main()
{
    A a{.b = 5};
}

Why?

Upvotes: 0

Views: 234

Answers (1)

Marek R
Marek R

Reputation: 38062

You are using feature "Designated initializers" which is available since C++20.

Also I can't reproduce this issue: https://godbolt.org/z/fz3PeP

  • note with C++17 gcc and clang just file an warning, msvc file an error
  • with C++20 it is fine on all three (msvc needs c++latest option).
  • with C++14 it files error everywhere

So looks like problem is just compiler version or configuration

Upvotes: 1

Related Questions