开发者社区> 问答> 正文

基类的对象作为派生类函数的形参传递。

class Point{
public:
Point(int xx,int yy){
x = xx;
y = yy;
}

private:
int x,y;
};
class Triangle:public Point{
public:
Triangle(Point p1,Point p2,Point p3):p1(p1),p2(p2),p3(p3){}
}
private:
Point p1;
Point p2;
Point p3;
};
上面两个类的定义在两个头文件中,在main函数中包括
#include 
#include
#include "Point.h"
#include "triangle.h"
using namespace std;
int main(){
Point point(7, 11); 
Point p1(1,1), p2(5,1), p3(1,4);
Triangle triangle(p1, p2, p3);
system("pause");
return 0;
}

错误是:triangle.h [Error] no matching function for call to 'Point::Point()'
这是什么原因?

展开
收起
a123456678 2016-03-04 16:38:52 2330 0
1 条回答
写回答
取消 提交回答
  • Triangle(Point p1,Point p2,Point p3): Point(oo,xx), p1(p1),p2(p2),p3(p3){}
    基类没有无参构造函数,需要指定使用基类的哪个构造函数。

    话说 三角形类从点类派生, 着实有些个蛋疼啊。。。楼主需要注意LSP原则

    2019-07-17 18:52:31
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
函数对象 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载