C++ 模板类的非成员比较运算符
我定义了一个模板容器树,它有两个成员类迭代器:const_迭代器和iterator 现在我想添加非成员比较运算符:C++ 模板类的非成员比较运算符,c++,templates,operator-keyword,non-member-functions,C++,Templates,Operator Keyword,Non Member Functions,我定义了一个模板容器树,它有两个成员类迭代器:const_迭代器和iterator 现在我想添加非成员比较运算符: template<typename T> bool operator==(Tree<T>::const_iterator a, Tree<T>::iterator b) { return a.ptr() == b.ptr(); } 为什么??这是由于模板造成的吗?您需要在此处使用typename,例如 template<typen
template<typename T>
bool operator==(Tree<T>::const_iterator a, Tree<T>::iterator b)
{
return a.ptr() == b.ptr();
}
为什么??这是由于模板造成的吗?您需要在此处使用typename,例如
template<typename T>
bool operator==(typename Tree<T>::const_iterator a, typename Tree<T>::iterator b)
// ~~~~~~~~ ~~~~~~~~
{
return a.ptr() == b.ptr();
}
您需要在此处使用typename,例如
template<typename T>
bool operator==(typename Tree<T>::const_iterator a, typename Tree<T>::iterator b)
// ~~~~~~~~ ~~~~~~~~
{
return a.ptr() == b.ptr();
}
啊。错误消息不是很明确。Thanks@galinette不幸的是,templates.Argh很常见。错误消息不是很明确。Thanks@galinette不幸的是,这在模板中很常见。即使缺少typename,您的重载也无法真正使用,因为无法推断T。方法是在Tree::const_迭代器中添加一个friend运算符==。即使缺少typename,也不能真正使用重载,因为无法推导T。方法是在Tree::const_迭代器中添加一个friend操作符==。