操作员重载功能模板中的链接器问题

时间:2019-06-29 21:28:17

标签: c++

我正在尝试在功能模板上重载运算符。我已经在代码开头声明了该函数 template <class type> fraction<type> operator+(fraction<type>, fraction<type>);,但是程序始终存在链接器错误。

函数主分数中引用的错误LNK2019无法解析的外部符号“分类分数__cdecl运算符+(分类分数,分类分数)”(?? H @ YA?AV?$ fraction @ H @@ V0 @ 0 @ Z)

#include <iostream>
#include <conio.h>
using namespace std;
template <class type> class fraction;
template <class type> fraction<type> operator+(fraction<type>, fraction<type>);
template <class type> class fraction
{
private:
    type numerator, denominator;
public:
    void setnumerator(type t)
    {
        numerator = t;
    }
    void setdenominatorr(type m)
    {
        denominator = m;
    }

    void input();
    void output();
    friend fraction<type> operator+(fraction<type>, fraction<type>);
    fraction(){}
    ~fraction(){}
};
template<class type> fraction<type>operator+(fraction<type> h1, fraction<type> h2)
{
    fraction<type> temp;
    temp.setnumerator(h1.numerator * h2.denominator + h2.numerator*h1.denominator);
    temp.setdenominatorr(h2.denominator*h1.denominator);
    return temp;
}

0 个答案:

没有答案