开发者

Unresolved external when using templates

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::BaseA开发者_C百科rrayIterator<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.


Which compiler are you using?

VS2010 is happy with this code:

namespace util
{
    template<typename T>
    class BaseArrayIterator
    {
    public:
        typedef ptrdiff_t difference_type;

        int pos;
        T x;
    };

    template<typename T>
    typename BaseArrayIterator<T>::difference_type operator -
        (const BaseArrayIterator<T> &itL,
         const BaseArrayIterator<T> &itR);

    template<typename T>
    typename BaseArrayIterator<T>::difference_type operator -
        (const BaseArrayIterator<T> &itL,
         const BaseArrayIterator<T> &itR)
    {
        return itL.pos - itR.pos;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    util::BaseArrayIterator<int> a;
    util::BaseArrayIterator<int> b;

    ptrdiff_t dist = a - b;
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜