c++中的模板问题

Problems with template in c++

本文关键字:问题 c++      更新时间:2023-10-16

可能重复:
模板运算符重载函数上的未定义符号

这是我的源代码。在Number.h

#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
using std::istream;
using std::ostream;
template <class T> class Number;
template <class T>
ostream& operator<<(ostream&, const Number<T>&);
template <class T>
istream& operator>>(istream&, Number<T>&);
template <class T>
class Number{
public:
    Number(const T &n)  :i(n) {}
    Number()            :i(0) {}
    T& operator+(const Number&rhs) const;
    T& operator-(const Number&rhs) const;
    T& operator*(const Number&rhs) const;
    T& operator/(const Number&rhs) const;
    friend ostream& operator<< <T> (ostream& , const Number<T>&);
    friend istream& operator>> <T> (istream& , Number<T>&);
private:
    T i;
};
#endif

并且在Number.cpp

#include "Number.h"
template<class T> 
T& Number<T>::operator+(const Number&rhs) const
{
    return i+rhs.i;
}
template<class T> 
T& Number<T>::operator-(const Number&rhs) const
{
    return i-rhs.i;
}
template<class T> 
T& Number<T>::operator*(const Number&rhs) const
{
    return i*rhs.i;
}
template<class T>
T& Number<T>::operator/(const Number&rhs) const
{
    return i/rhs.i;
}
template<class T>
ostream& operator<<(ostream&os , const Number<T>&rhs)
{
    return os<< rhs.i;
}
template<class T>
istream& operator>>(istream&is , Number<T>&rhs)
{
    return is >> rhs.i;
}

我不知道为什么有

undefined reference to `std::istream& operator>><double>(std::istream&,Number<double>&)'
undefined reference to `Number<double>::operator+(Number<double> const&) const'

错误等等

使用.hpp作为模板,不能返回对临时对象的引用。

number.h

#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
using std::istream;
using std::ostream;
template <class T> class Number;
template <class T>
ostream& operator<<(ostream&, const Number<T>&);
template <class T>
istream& operator>>(istream&, Number<T>&);
template <class T>
class Number{
        public:
                Number(const T &n)  :i(n) {}
                Number()            :i(0) {}
                T operator+(const Number&rhs) const; // Error Here return T not T&
                T operator-(const Number&rhs) const;
                T operator*(const Number&rhs) const;
                T operator/(const Number&rhs) const;
                friend ostream& operator<< <T> (ostream& , const Number<T>&);
                friend istream& operator>> <T> (istream& , Number<T>&);
        private:
                T i;
};
#include <number.hpp>
#endif

平均

#ifndef NUMBER_HPP
#define NUMBER_HPP
template<class T> 
T
Number<T>::operator+(const Number& rhs) const
{
        return i + rhs.i;
}
template<class T> 
T
Number<T>::operator-(const Number&rhs) const
{
        return i-rhs.i;
}
template<class T> 
T
Number<T>::operator*(const Number&rhs) const
{
        return i*rhs.i;
}
template<class T>
T
Number<T>::operator/(const Number&rhs) const
{
        return i/rhs.i;
}
template<class T>
ostream& operator<<(ostream&os , const Number<T>&rhs)
{
        return os<< rhs.i;
}
template<class T>
istream& operator>>(istream&is , Number<T>&rhs)
{
            return is >> rhs.i;
}
#endif

main.cpp

    #include <iostream>
    #include <number.h>
    int
    main(int, const char**)
    {
        Number<double>  value(1);
        Number<double>  add(3);
        std::cout << value + add << std::endl;
        std::cout << value * add << std::endl;
        std::cout << value - add << std::endl;
        std::cout << value / add << std::endl;
        return 0;
}

所有这些成员函数的定义都需要可用于实例化模板的任何转换单元。想象一个包含Number.h并尝试使用Number<int>的文件。编译器然后需要生成用T实例化的Number的所有代码作为int。如果它只看到Number.h,它怎么能做到这一点?它不知道成员函数的定义。

修复方法是将成员函数的定义(从Number.cpp开始的所有内容(放在Number.h中。或者,有些人喜欢将Number.cpp命名为Number.tpp,并将#include "Number.tpp"放在Number.h底部,而不是Number.cpp中的#include "Number.h",这基本上颠倒了包含,因此标头始终包含实现。