oYinShuiSiYuan1234 2021-06-18 17:31 采纳率: 100%
浏览 22
已采纳

类模板的多重继承,为什么在调用基类构造函数时会出错?

#include<iostream>
using namespace std;
template<class T>
class Point
{
public:
	T x, y;
	Point(T a, T b) :x(a),y(b){}
};
template<class T>
class Square : public Point<T>
{
public:
	T a;
	Square(T a,T b,T aa):Point(a,b),a(aa) {}
};
template<class T>
class Circle :public Point<T>
{
public:
	T r;
	Circle(T a, T b, T aa) :Point(a, b), r(aa) {}
};
template<class T>
class Solve :public Square<T>, public Circle<T>
{
public:
	void solve()
	{
		const T dis = Square::a / 2 + Circle::r;
		const T dx = abs(Square::x - Circle::x);
		const T dy = abs(Square::y - Circle::y);
		if (dis < dx && dis < dy)
			cout << "hh";
		else
			cout << "qq";
	}
public:
	Solve(T x1,T y1,T x2,T y2,T a,T r) : Square(x1,y1,a), Circle(x2,y2,r) {}
};
int main()
{
	double x1, y1, x2, y2, a, r;
	cin >> x1 >> y1 >> x2 >> y2 >> a >> r;
	Solve<double> s(x1, y1, x2, y2, a, r);
	
}

Circle Square都是Point的派生类,Solve继承于Circle Square,在调用他们的构造函数出错,报错为非法成员初始化,请问是什么原因?

  • 写回答

1条回答 默认 最新

  • oYinShuiSiYuan1234 2021-06-18 21:00
    关注

    当类被设置为类模板的以后,类名就变成了Square<T>,将上述代码中的Square替换即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料