no appropriate default constructor available C++编程

来源:百度知道 编辑:UC知道 时间:2024/07/08 07:59:35
#include<iostream.h>

class shape
{ protected : double x,y;
public: shape(double i, double j=0) { x = i ; y = j ; }
~shape(){cout << "~shape() is called.\n" ; }
virtual void area() = 0 ;
};
class triangle : public shape
{ public:

triangle(double i, double j) { x = i ; y = j ; cout << "triangle() is called.\n" ;}
virtual ~triangle(){cout << "~triangle() is called.\n" ; }
void area()
{ cout<<"Triangle with high "<<x<<" and base "<<y <<" has an area of "<<x*0.5*y<<"\n"; }
};
class square : public shape
{ public:
square(double i, double j) { x = i ; y = j ; cout << "square() is called.\n" ;}
virtual ~square(){cout << "~square() is called.\n" ; }

shape 没有默认构造函数,给它加个
shape(){}就可以了

在派生类的构造函数里,会自动调用父类的缺省构造函数,由于你自己提供了构造函数 所以系统将不会再提供默认的构造函数也就是default constructor
所以在基类shape添加上shape(){}就好了


shape(){}
替换
shape(double i, double j=0) { x = i ; y = j ; }
就行