复制构造函数里使用模板类对象作为参数

snowdesert 2008-10-13 11:40:53
复制构造函数里使用模板类对象作为参数,有一句报错,怎么改?


#include <iostream>
using namespace std;

template <typename T>
class Point
{
T x,y;
public:
Point(T x1,T y1){x=x1;y=y1;cout<<x<<","<<y<<endl;}
Point(const Point& p1){x=p1.x;y=p1.y;}
};

template <typename T>
class LineDerive:public Point<T>
{
T x2,y2;
public:
LineDerive(T i1,T j1,T i2,T j2):Point<T>(i1,j1){x2=i2;y2=j2;}
};

template <typename T>
class LineInclude
{
Point<T> p1,p2;
public:
LineInclude(T i1,T j1,T i2,T j2):p1(i1,j1),p2(i2,j2){}
LineInclude(const Point& pt1,const Point& pt2):p1(pt1),p2(pt2){}
};

void main()
{
Point<int> p1(1,2),p2(3,4);
LineDerive<int> ld1(5,6,7,8);
LineInclude<int> li1(9,10,11,12);
LineInclude<int> li2(p1,p2); //这一句报错,怎么改?
}
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jia_xiaoxin 2008-10-14
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

template <typename T>
class Point
{
T x,y;
public:
Point(T x1,T y1){x=x1;y=y1;cout<<x<<","<<y<<endl;}
Point(const Point& p1){x=p1.x;y=p1.y;}
};

template <typename T>
class LineDerive:public Point<T>
{
T x2,y2;
public:
LineDerive(T i1,T j1,T i2,T j2):Point<T>(i1,j1){x2=i2;y2=j2;}
};

template <typename T>
class LineInclude
{
Point<T> p1,p2;
public:
LineInclude(T i1,T j1,T i2,T j2):p1(i1,j1),p2(i2,j2){}
LineInclude(const Point<T>& pt1,const Point<T>& pt2):p1(pt1),p2(pt2){}//参数类型因该为模板类型
};

void main()
{
Point<int> p1(1,2),p2(3,4);
LineDerive<int> ld1(5,6,7,8);
LineInclude<int> li1(9,10,11,12);
LineInclude<int> li2(p1,p2); //这一句报错,怎么改?
}
snowdesert 2008-10-14
  • 打赏
  • 举报
回复
谢谢大家 :)
yshuise 2008-10-14
  • 打赏
  • 举报
回复
1,3都是对的
lzr4304061988012 2008-10-13
  • 打赏
  • 举报
回复

1,2
3,4
5,6
9,10
11,12
请按任意键继续. . .
lzr4304061988012 2008-10-13
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

template <typename T>
class Point
{
T x,y;
public:
Point(T x1,T y1){x=x1;y=y1;cout<<x<<","<<y<<endl;}
Point(const Point& p1){x=p1.x;y=p1.y;}
};

template <typename T>
class LineDerive:public Point<T>
{
T x2,y2;
public:
LineDerive(T i1,T j1,T i2,T j2):Point<T>(i1,j1){x2=i2;y2=j2;}
};

template <typename T>
class LineInclude
{
Point<T> p1,p2;
public:
LineInclude(T i1,T j1,T i2,T j2):p1(i1,j1),p2(i2,j2){}
LineInclude(const Point<T>& pt1,const Point<T>& pt2):p1(pt1),p2(pt2){}//here
};

void main()
{
Point<int> p1(1,2),p2(3,4);
LineDerive<int> ld1(5,6,7,8);
LineInclude<int> li1(9,10,11,12);
LineInclude<int> li2(p1,p2); //这一句报错,怎么改?
}


64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧