类模板的问题
#include <iostream>#include<cassert>
using namespace std;
template <typename T>
class Point
{
T x,y;
public:
Point(T xi=0,T yi=0):x(xi),y(yi){}
friend double slope(Point<T> &a,Point<T> &b);
~Point(){}
};
template<typename T>
double Point<T>::slope(Point<T> &a,Point<T> &b)//求个斜率
{
assert(a.x!=b.x);
double k=(a.y-b.y)/(a.x-b.x);
return k;
}
int main()
{
Point<int> point1(1,1);
Point<int> point2(2,2);
cout<<slope(point1,point2)<<endl;
return 0;
}
错误:
E:\codeBlock\模板的测试\main.cpp|16|error: no 'double Point<T>::slope(Point<T>&, Point<T>&)' member function declared in class 'Point<T>'|
他说没在Point<T>中申明,不懂。是我定义错了?翻书查,没错啊。我眼拙看不出,求赐教啊。