Undefined reference when trying to define template <typename T> operator<

30 views Asked by At

I am trying to implement my own BigInteger class with overloaded operators. So I have

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

which works OK, and now I try to overload operator< also as following:

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

But in main.cpp following code

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

cannot compile, error undefined reference to 'bool numeric::types::operator< <int>(numeric::types::BigInteger const&, int const&)'

P.S. BigInteger is in namespace numeric::types {}

BigInteger of course has int constructor:

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

Is it possible to somehow make this work?

P.P.S. This is not anyhow connected with Why can templates only be implemented in the header file?, because BigInteger is just class BigInteger, not a template <typename T> class BigInteger

So in code

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

both of cout's should work correctly

0

There are 0 answers