大佬教程收集整理的这篇文章主要介绍了C错误:没有适当的默认构造函数可用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#include "stdafx.h" #include "iostream" #include "String" #include "cmath" #include "ctime" using namespace std; class quad{ int Lice; public: quad(int x,int z){ Lice = x*z; } int ShowLice(){ return Lice; } }; class tri : public quad{ int Lice; public: tri(int a,int b,int C){ Lice = a*b*c; } }; int main(){ quad ob1(2,2); tri ob2(2,2,2); cout<<ob1.ShowLice(); cout<<ob2.ShowLice(); return 0; }
我使用VS2008,编译器的错误是:
project1.cpp(20) : error C2512: 'quad' : no appropriate default constructor available
谢谢,Leron.
class quad{ protected: int Lice; public: quad(int x,int z){ Lice = x*z; } int ShowLice(){ return Lice; } }; class tri : public quad{ public: tri(int a,int C) : quad(a,b) { Lice *= c; } };
在派生类和基类中都没有Lice成员.使其受到保护,以便派生类和子类重用相同的字段.
如果在基类中有非默认构造函数,则必须使用派生类构造函数中的正确参数调用基类构造函数,如上所示.
此外,如果您使用继承,最好在基类中提供virtual destructor.您的示例代码中可能不会出现此问题.
以上是大佬教程为你收集整理的C错误:没有适当的默认构造函数可用全部内容,希望文章能够帮你解决C错误:没有适当的默认构造函数可用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。