| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1922 人关注过本帖
标题:[求助]一个类模板问题
取消只看楼主 加入收藏
vicbey
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2004-12-22
收藏
 问题点数:0 回复次数:2 
[求助]一个类模板问题

初学

十分不解

多多帮助

谢谢

#include<iostream> #include<math.h> using namespace std; template<class T> class Point{ T x,y; public: Point(T a=0.0,T b=0.0){x=a;y=b;} T Distance(Point<T>&); void Display(Point<T>&); ~Point(){cout<<"Delete Point."<<endl; } };

template<class T> class Line{ Point<T>A,B; public: Line(Point<T>&,Point<T>&); T Display(Line&); ~Line(){cout<<"Delete Line"<<endl;} };

template<class T> Point<T>::Distance(Point<T>& a) { T l; l=pow((pow((x-a.x),2)+pow((y-a.y),2)),0.5); return l; }

template<class T> Point<T>::Display(Point<T>& a) {cout<<a.x<<","<<a.y<<endl;}

template<class T> Line<T>::Line(Point<T>& a,Point<T>& b){ A=a; B=b; cout<<A.Display<<endl<<B.Display<<endl; }

template<class T> Line<T>::Display(Line& s){ cout<<s.A.Distance(s.B)<<endl; }

void main(){ Point<double> a; Point<double> b(7.8,9.8),c(34.5,67.8); a=c; cout<<"两点距离是:"<<A.Distance(B)<<endl; Line<double>s(A,B); cout<<s.Display(s)<<endl; }

错误:e:\vc++6\my document\类模板\tt.cpp(30) : error C2244: 'Point<T>::Distance' : unable to resolve function overload e:\vc++6\my document\类模板\tt.cpp(32) : error C2954: template definitions cannot nest e:\vc++6\my document\类模板\tt.cpp(34) : error C2244: 'Point<T>::Display' : unable to resolve function overload e:\vc++6\my document\类模板\tt.cpp(36) : error C2954: template definitions cannot nest e:\vc++6\my document\类模板\tt.cpp(46) : error C2244: 'Line<T>::Display' : unable to resolve function overload e:\vc++6\my document\类模板\tt.cpp(50) : error C2514: 'Point<double>' : class has no constructors e:\vc++6\my document\类模板\tt.cpp(50) : error C2262: 'b' : cannot be destroyed e:\vc++6\my document\类模板\tt.cpp(50) : error C2514: 'Point<double>' : class has no constructors e:\vc++6\my document\类模板\tt.cpp(50) : error C2262: 'c' : cannot be destroyed e:\vc++6\my document\类模板\tt.cpp(51) : error C2065: 'a' : undeclared identifier e:\vc++6\my document\类模板\tt.cpp(51) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Point<double>' (or there is no acceptable conversion) e:\vc++6\my document\类模板\tt.cpp(52) : error C2065: 'A' : undeclared identifier e:\vc++6\my document\类模板\tt.cpp(52) : error C2228: left of '.Distance' must have class/struct/union type e:\vc++6\my document\类模板\tt.cpp(52) : error C2065: 'B' : undeclared identifier e:\vc++6\my document\类模板\tt.cpp(53) : error C2514: 'Line<double>' : class has no constructors e:\vc++6\my document\类模板\tt.cpp(53) : error C2262: 's' : cannot be destroyed e:\vc++6\my document\类模板\tt.cpp(54) : error C2027: use of undefined type 'Line<double>' e:\vc++6\my document\类模板\tt.cpp(54) : error C2228: left of '.Display' must have class/struct/union type Error executing cl.exe.

tt.obj - 18 error(s), 0 warning(s)

搜索更多相关主题的帖子: class 模板 Line Point 
2004-12-22 18:08
vicbey
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2004-12-22
收藏
得分:0 

我看好象也是不能嵌套的意思

但我怎么样才能让一个point的类当他自己的一个参数呢?

我要做的目的是定义两个类模板(point和line) 然后求线段的距离 怎么也做不出来

多谢高手帮助

2004-12-22 23:52
vicbey
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2004-12-22
收藏
得分:0 

多谢VC6程序员啊!!

真是太感谢了 因为初学 也没老师 书上的例子也不多 幸亏大虾相救! 这让我有继续学下去的动力了!!

2004-12-23 18:43
快速回复:[求助]一个类模板问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017745 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved