注册 登录
编程论坛 C++教室

类模板的问题

moox 发布于 2018-02-04 23:03, 1920 次点击
#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>中申明,不懂。是我定义错了?翻书查,没错啊。我眼拙看不出,求赐教啊。
7 回复
#2
wengbin2018-02-04 23:54
slope函数定义也拿出来看看吧
#3
wengbin2018-02-05 00:12
看到了,那个slop函数并非类Point的成员函数,你的slop函数写的有问题,double Point<T>::slope(Point<T> &a,Point<T> &b)改成T slope(Point<T> &a,Point<T> &b)试试
#4
rjsp2018-02-05 09:05
如果要严谨些,那得这么写
程序代码:
#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;
}

差别在于,第一段代码中 slope<int>是Point<int>的友元,slope<double>是Point<double>的友元,……
而第二段代码中,slope<int>、slope<double>……是Point<int>的友元,也是Point<double>的友元,……

#5
rjsp2018-02-05 09:28
我还是觉得你过度设计了,应该简单点儿
程序代码:
#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;
}

#6
moox2018-02-05 10:54
回复 4楼 rjsp
谢谢,很详细
#7
moox2018-02-05 10:55
回复 3楼 wengbin
谢谢,确实是这儿的毛病,我友元函数知识不精所导致
#8
moox2018-02-05 10:57
回复 5楼 rjsp
谢谢
1