初学
十分不解
多多帮助
谢谢
#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)