-3 c++ mfc visual-c++
我收到错误C2512:'derived':派生类的构造函数定义中没有适当的默认构造函数可用错误.我的代码如下.我该如何解决这个问题?
Class A
{
int a, int b;
A(int x, int y)
{
sme code....
}
}
Class B
{
int a, int b, int c;
B(int x, int y, int Z)
{
sme code....
}
}
Class derived : public A, public B
{
derived(int a, int b):A(a, b)
{
}
derived(int a, int b, int c):B(a, b, c)
{
}
}
Run Code Online (Sandbox Code Playgroud)
其中一个问题是,在每个派生类的构造函数中,您只将适当的构造函数参数转发给两个基类中的一个.他们谁也没有一个默认的构造函数,所以你需要两个基类的构造,以明确提供的参数A和B.
第二个问题是基类的构造函数被隐式声明为private,因此基类无法访问它们.你应该public或者至少制作它们protected.
小问题:在类定义之后,你需要加一个分号.另外,声明类的关键字class不是Class.
class A // <---- Use the "class" keyword
{
public: // <---- Make the constructor accessible to derived classes
int a, int b;
A(int x, int y)
{
some code....
}
}; // <---- Don't forget the semicolon
class B // <---- Use the "class" keyword
{
public: // <---- Make the constructor accessible to derived classes
int a, int b, int c;
B(int x, int y, int Z)
{
sme code....
}
}; // <---- Don't forget the semicolon
// Use the "class" keyword
class derived : public A, public B
{
derived(int a, int b) : A(a, b), B(a, b, 0) // <---- for instance
{
}
derived(int a, int b, int c) : B(a, b, c), A(a, b) // <---- for instance
{
}
}; // <---- Don't forget the semicolon
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
914 次 |
| 最近记录: |