尝试定义模板<typename T>运算符<时未定义的引用

问题描述

我正在尝试使用重载运算符实现自己的BigInteger类。所以我有

bool operator<(const BigInteger& left,const BigInteger& right) {
  // some code
}

工作正常,现在我也尝试重载operator<,如下所示:

template <typename T>
bool operator<(const BigInteger& left,const T& right) {
  BigInteger big_right(right);
  return (left < big_right);
}

但是在 main.cpp 下面的代码

numeric::types::BigInteger a{17};
std::cout << std::boolalpha;
std::cout << (a < 16) << std::endl;

无法编译,错误undefined reference to 'bool numeric::types::operator< <int>(numeric::types::BigInteger const&,int const&)'

P.S。 BigIntegernamespace numeric::types {}

BigInteger当然具有int构造函数

BigInteger::BigInteger(int int_number) {
  // some code
}

是否有可能使这项工作奏效?

P.P.S。这与Why can templates only be implemented in the header file?无关,因为BigInteger只是class BigInteger,而不是template <typename T> class BigInteger

所以在代码

numeric::types::BigInteger a{10};
std::cout << (a < 2) << std::endl;
std::cout << (a < 2LL) << std::endl;

两个cout都应该正常工作

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)