I'm mixing operator redefinition with template classes and reached to the following assigment:
j = end - begin;
in my main function, where variable types are as follows:
ptrdiff_t j;
util::BaseArrayIterator<int, 6> begin, end;
Begin and end have been initialized from a util::BaseArray:
util::BaseArray<int, 6> ba(SOME_PARAMETERS);
begin = ba.begin(); end = ba.end();
BaseArrayIterator is a self-implemented iterator type.
I get the error:
TestProject.obj : error LNK2019: unresolved external symbol "int __cdecl util::operator-(class util::BaseArrayIterator<int,6> const &,class util::BaseArrayIterator<int,6> const &)" (??Gutil@@YAHABV?$BaseArrayIterator@H$05@0@0@Z) referenced in function _main
due to the first code statement in the message (removing it fixes the problem).
I have included a header file with the following definitions and declarations:
namespace util {
template<typename T, int n>
typename BaseArrayIterator<T,n>::difference_type operator -
(const BaseArrayIterator<T,n> &itL,
const BaseArrayIterator<T,n> &itR);
...
template<typename T, int n>
typename BaseArrayIterator<T,n>::difference_type operator -(
const BaseArrayIterator<T,n> &itL,
const BaseArrayIterator<T,n> &itR)
{ return -(itL.m_cnt - itR.m_cnt);
}
}
What causes the problem? The compiler reports searching for a util::operator -, so he did find the declaration, but not the definition, although they are in the same file. And I see no signature mistake.
--- EDIT NOTE -----------------------------------------------------------------------------
Replacing
end-begin
with
util::operator-<int, 6>(end,begin)
solves the problem, but I don't want to explicitly specify the parameters each time. Concision is one of the main arguments in favor of overloading operator, so I'd like to use the classic short form.
--- EDIT NOTE 2 ---------------------------------------------------------------------------
As Nicola Mussatti kindly indicated, [a solution]: Unresolved external symbol with operator overloading and templates to the problem is here. Friend declaration should be moved inside the class.
So i did and I'm all smiles. Now separating them back again issues an ambiguous overload issues, which is not the same error as previously.