When I'm trying to create multiple constructors for an inherited class, I have an error throw out saying: No matching function call to A::A(). My codes are shown as below:
class A{
public:
int a;
int b;
A(int i, int k) : a(i), b(k){
};
};
class B : public A{
public:
B(){
};
B(int i, int k) : A(i, k){
};
};
B() {}
is equivalent to
B() : A() {}
Since A
doesn't have a default constructor, that is a compile time error.
You can fix it by:
A
, or By changing the implementation of B
's default constructor to use the existing constructor of A
.
B() : A(0, 0) {}
How many constructors can an inherited class have?
The language does not impose a limit on the number of constructors whether the class inherits another or not (except if you inherit a class with no non-deleted constructors, then the child also cannot have any non-deleted constructors).
The implementation may be constrained in practice. The minimum number of maximum supported member declarations - which includes the constructors - for a single class recommended by the standard is 4096. Being a recommendation means that neither lower nor higher supported maximum affects whether an implementation may be considered to be standard compliant.
No matching function call to A::A()
This is because you're trying to default initialise the base class sub object, but the base class is not default-initialisable.
There are two possible solutions:
B(): A(42, 1337) {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.