![](/img/trans.png)
[英]Why am I getting errors about int not matching unsigned long?
[英]why am I getting the following errors : In constructor 'B::B(int, int)': no matching function for call to 'A::A()'
我遇到错误,不知道出什么问题了
我试过省略构造函数。
我收到以下错误:
在构造函数'B :: B(int,int)'中:没有匹配的函数可以调用'A :: A()'
注意候选人是:
A :: A(const A&)
A :: A(int,int)
#include <iostream>
using namespace std;
class A{
public:
int a;
int b;
A(int a1, int b1){
a=a1; b = b1;
}
};
class B : public A {
public:
int c;
int d;
int e;
B(int c1=10, int d1=20){
c=c1; d=d1;
e = a * b;
}
void show(){
cout <<"a = "<<a<<endl;
cout <<"b = "<<b<<endl;
cout <<"c = "<<c<<endl;
cout <<"d = "<<d<<endl;
cout <<"e = "<<e<<endl;
}
};
int main() {
A a(2,2);
B b;
b.show();
return 0;
}
B
继承A
所以它需要构建A
,但你没有为默认的构造函数A
你也不显式调用构造A
从B
的初始化列表。
您需要这样的东西:
B(int c1=10, int d1=20) : A(c1, d1) {
c=c1; d=d1;
e = a * b;
}
或使A
默认构造。
另外,您还应该对c
, d
和e
使用初始化列表,而不是在构造函数的主体中分配给in(尽管优化器会在这种情况下进行处理,但最好使用以下形式:一直正确的方式)
B(int c1=10, int d1=20) : A(c1, d1), c(c1), d(d1), e(a*b) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.