| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

已结贴   问题点数：20  回复次数：7

#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>'|

得分:10
slope函数定义也拿出来看看吧
得分:0

得分:10

#include <cassert>

template<typename T>
class Point;

template<typename T>
double slope( const Point<T>& a, const Point<T>& b );

template<typename T>
class Point
{
public:
Point() : x_(), y_()
{
}
Point( T x, T y ) : x_(x), y_(y)
{
}

private:
T x_, y_;

friend double slope<>( const Point<T>& a, const Point<T>& b );
};

template<typename T>
double slope( const Point<T>& a, const Point<T>& b )
{
assert( a.x_ != b.x_ );
return (a.y_-b.y_)/(a.x_-b.x_);
}

#include <iostream>
using namespace std;

int main( void )
{
Point<int> point1(1,1);
Point<int> point2(2,2);
cout << slope(point1,point2) << endl;
return 0;
}

#include <cassert>

template<typename T>
class Point
{
public:
Point() : x_(), y_()
{
}
Point( T x, T y ) : x_(x), y_(y)
{
}

private:
T x_, y_;

template<typename U> friend double slope( const Point<U>& a, const Point<U>& b );
};

template<typename U>
double slope( const Point<U>& a, const Point<U>& b )
{
assert( a.x_ != b.x_ );
return (a.y_-b.y_)/(a.x_-b.x_);
}

#include <iostream>
using namespace std;

int main( void )
{
Point<int> point1(1,1);
Point<int> point2(2,2);
cout << slope(point1,point2) << endl;
return 0;
}

得分:0

#include <cassert>

template<typename T>
struct Point
{
T x, y;

Point() : x(), y()
{
}
Point( T x, T y ) : x(x), y(y)
{
}
};

template<typename T, typename U>
double slope( const T& a, const U& b )
{
assert( a.x != b.x );
return (a.y-b.y)/(a.x-b.x+0.0);
}

#include <iostream>
using namespace std;

int main( void )
{
Point<int> point1(1,1);
Point<int> point2(2,2);
cout << slope(point1,point2) << endl;
return 0;
}

得分:0

得分:0

得分:0

• 8
• 1/1页
• 1