ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

用Cpp进行泛型编程时需要注意一个小问题

2021-12-29 12:31:21  阅读:177  来源: 互联网

标签:__ denominator const Rationalt 编程 numerator Cpp template 泛型


        当在一个模板类中,需要使用别的.h头文件中的函数模板时,一定需要在别的.h头文件中加上这样2句话:(否则就是报错!这样的错误你很难发现是啥错,也难以看懂!)

        这个小技巧一定要掌握!!!

template<typename T>
class 对应模板类的名称;//注意:不需要带上T,否则会报错!

//给这个模板类来一个前置声明,否则后续该模板函数用到该类时会识别不出来是哪一个类

rationalt.h:

#ifndef __RATIONALT_H__
#define __RATIONALT_H__
#include<iostream>
#include<memory>
#include"domultiply.h"
using namespace std;
template<typename T>
class Rationalt{
public:
    typedef Rationalt<T> RaT;
    friend const RaT operator*(const RaT& rhs1, const RaT& rhs2){
        return doMultiply(rhs1,rhs2);
        //为了让inline的冲击最小化,必须这么干!(来一个辅助函数do事情!而operator*里面不do任何事情)
}
public:
    inline Rationalt(const T& numerator = 0,const T& denominator = 1)
        :m_Numerator(new int(numerator)),m_Denominator(new int(denominator)){}
    Rationalt(const Rationalt& rhs);
    const RaT& operator=(const RaT& rhs);
    // const Rationalt& operator*(const Rationalt& rhs);
    inline const T numerator()const { return *m_Numerator;}
    inline const T denominator()const{ return *m_Denominator;}
    void reviseNumerator(T val); 
    void reviseDenominator(T val);
    inline ~Rationalt(){
        this->m_Numerator = nullptr;//置为nullpr!
        this->m_Denominator = nullptr;//置为nullpr!
    }
private:
    std::shared_ptr<T> m_Numerator;
    std::shared_ptr<T> m_Denominator;
};
template<typename T>
Rationalt<T>::Rationalt(const Rationalt<T>& rhs){
    this->m_Numerator.reset();
    m_Numerator = make_shared<T>(rhs.numerator());
    this->m_Denominator.reset();
    m_Denominator = make_shared<T>(rhs.denominator());
}
template<typename T>
const Rationalt<T>& Rationalt<T>::operator=(const Rationalt<T>& rhs){
    this->m_Numerator.reset();
    m_Numerator = make_shared<T>(rhs.numerator());
    this->m_Denominator.reset();
    m_Denominator = make_shared<T>(rhs.denominator());
    return *this;
}
template<typename T>
// const Rationalt& operator*(const Rationalt& rhs);
void Rationalt<T>::reviseNumerator(T val){
    this->m_Numerator.reset();
    m_Numerator = make_shared<T>(val);
}
template<typename T>
void Rationalt<T>::reviseDenominator(T val){
    this->m_Denominator.reset();
    m_Denominator = make_shared<T>(val);
}
#endif //__RATIONALT_H__

domultiply.h:

#ifndef __DOMULTIPLY_H__
#define __DOMULTIPLY_H__
#include"rationalt.h"
template<typename T> 
class Rationalt;//不用写为class Rationalt<T>;这样写反而会报错!!!
template<typename T>
const Rationalt<T> doMultiply(const Rationalt<T>& rhs1, const Rationalt<T>& rhs2){
    return Rationalt<T>(rhs1.numerator() * rhs2.numerator(),
                    rhs1.denominator() * rhs2.denominator());
}
#endif //__DOMULTIPLY_H__

运行结果:(显示正常)

Error_demo1_codes:

#ifndef __DOMULTIPLY_H__
#define __DOMULTIPLY_H__
#include"rationalt.h"
template<typename T>
const Rationalt<T> doMultiply(const Rationalt<T>& rhs1, const Rationalt<T>& rhs2){
    return Rationalt<T>(rhs1.numerator() * rhs2.numerator(),
                    rhs1.denominator() * rhs2.denominator());
}
#endif //__DOMULTIPLY_H__

不加这2句话的运行结果:

Error_demo2_codes:

#ifndef __DOMULTIPLY_H__
#define __DOMULTIPLY_H__
#include"rationalt.h"
template<typename T> 
class Rationalt<T>;//不用写为class Rationalt<T>;这样写反而会报错!!!
template<typename T>
const Rationalt<T> doMultiply(const Rationalt<T>& rhs1, const Rationalt<T>& rhs2){
    return Rationalt<T>(rhs1.numerator() * rhs2.numerator(),
                    rhs1.denominator() * rhs2.denominator());
}
#endif //__DOMULTIPLY_H__

加这2句话但是还加上模板参数T的运行结果:

标签:__,denominator,const,Rationalt,编程,numerator,Cpp,template,泛型
来源: https://blog.csdn.net/weixin_44980842/article/details/122212585

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有