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

初学

十分不解

多多帮助

谢谢

#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
BlueDreame
Rank: 1
等 级:新手上路
帖 子:545
专家分:2
注 册:2004-12-16
收藏
得分:0 
我不太熟悉模板,但就错误信息看,模板类不能嵌套。

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

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

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

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

多谢高手帮助

2004-12-22 23:52
C++大粉丝
Rank: 4
等 级:贵宾
威 望:10
帖 子:477
专家分:0
注 册:2004-4-23
收藏
得分:0 

#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> 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> void 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; A.Display(a); B.Display(b); }

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

void main() { }


I am a big fan of c plus plus.
2004-12-23 08:46
C++大粉丝
Rank: 4
等 级:贵宾
威 望:10
帖 子:477
专家分:0
注 册:2004-4-23
收藏
得分:0 

我不知道你要做什么,所以main里我什么也没有写。

这段程序的主要问题是你的成员函数没有写返回值...

还有返回是void的函数你不能在用cout了,你应该知道的,估计是马虎吧...

要不就是你想输出函数的地址...


I am a big fan of c plus plus.
2004-12-23 08:52
vicbey
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2004-12-22
收藏
得分:0 

多谢VC6程序员啊!!

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

2004-12-23 18:43
fatfish
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2004-12-26
收藏
得分:0 
还有返回是void的函数你不能在用cout了?
可以用的,我经常用

2004-12-27 20:53
C++大粉丝
Rank: 4
等 级:贵宾
威 望:10
帖 子:477
专家分:0
注 册:2004-4-23
收藏
得分:0 

是吗?

给个例子.


I am a big fan of c plus plus.
2004-12-28 08:36
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

#include<iostream> #include<cmath> 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(const Point<T>&) const; void Display() const; ~Point(){cout<<"Delete Point."<<endl;} };

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

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

template<class T> void Point<T>::Display() const {cout<<x<<","<<y<<endl;}

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

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

int main() { Point<double> a; Point<double> b(7.8,9.8),c(34.5,67.8); a=c; cout<<"The two Point's distance is: "<<a.Distance(b)<<endl; Line<double>s(a,b); s.Display();

return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-12-28 09:38
快速回复:[求助]一个类模板问题
数据加载中...
 
   



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

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