| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1568 人关注过本帖
标题:类模板的问题
只看楼主 加入收藏
moox
Rank: 2
来 自:福建
等 级:论坛游民
帖 子:92
专家分:93
注 册:2017-1-21
结帖率:82.35%
收藏
已结贴  问题点数: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>'|
他说没在Point<T>中申明,不懂。是我定义错了?翻书查,没错啊。我眼拙看不出,求赐教啊。
搜索更多相关主题的帖子: 类模板 Point int double main 
2018-02-04 23:03
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:10 
slope函数定义也拿出来看看吧
2018-02-04 23:54
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:0 
看到了,那个slop函数并非类Point的成员函数,你的slop函数写的有问题,double Point<T>::slope(Point<T> &a,Point<T> &b)改成T slope(Point<T> &a,Point<T> &b)试试
2018-02-05 00:12
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分: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;
}

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

2018-02-05 09:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分: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;
}

2018-02-05 09:28
moox
Rank: 2
来 自:福建
等 级:论坛游民
帖 子:92
专家分:93
注 册:2017-1-21
收藏
得分:0 
回复 4楼 rjsp
谢谢,很详细
2018-02-05 10:54
moox
Rank: 2
来 自:福建
等 级:论坛游民
帖 子:92
专家分:93
注 册:2017-1-21
收藏
得分:0 
回复 3楼 wengbin
谢谢,确实是这儿的毛病,我友元函数知识不精所导致
2018-02-05 10:55
moox
Rank: 2
来 自:福建
等 级:论坛游民
帖 子:92
专家分:93
注 册:2017-1-21
收藏
得分:0 
回复 5楼 rjsp
谢谢
2018-02-05 10:57
快速回复:类模板的问题
数据加载中...
 
   



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

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