使用模板时未解决的外部问题
我将运算符重定义与模板类混合在一起,并达到以下分配:
j = end - begin;
在我的主函数中,其中变量类型如下:
ptrdiff_t j;
util::BaseArrayIterator<int, 6> begin, end;
Begin 和 end 已从 util::BaseArray 初始化:
util::BaseArray<int, 6> ba(SOME_PARAMETERS);
begin = ba.begin(); end = ba.end();
BaseArrayIterator 是一个自行实现的迭代器类型。
我收到错误:
TestProject.obj:错误 LNK2019:无法解析的外部符号 “int __cdecl util::operator-(class util::BaseArrayIterator
; const &,class util::BaseArrayIterator ;常量&)" (??Gutil@@YAHABV?$BaseArrayIterator@H$05@0@0@Z) 中引用 函数_main
由于消息中的第一个代码语句(删除它可以解决问题)。
我已经包含了一个包含以下定义和声明的头文件:
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);
}
}
是什么导致了问题? 编译器报告搜索 util::operator -,因此他确实找到了声明,但没有找到定义,尽管它们位于同一个文件中。而且我没有发现签名错误。
--- 编辑注释 ---------------------------------------------- --------------------------------
替换
end-begin
为
util::operator-<int, 6>(end,begin)
解决了问题,但我不想显式指定每个参数时间。简洁是支持重载运算符的主要论据之一,因此我想使用经典的简短形式。
--- 编辑注释 2 -------------------------------------------------------- -------------------------------
正如 Nicola Mussatti 善意指出的那样,[解决方案]: 具有运算符重载和模板的未解析的外部符号问题就在这里。好友声明应移至类内。
所以我就这么做了,我满面笑容。 现在将它们再次分离会引发一个不明确的过载问题,这与之前的错误不同。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用哪个编译器?
VS2010 对这段代码很满意:
Which compiler are you using?
VS2010 is happy with this code: