Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板类的非成员比较运算符_C++_Templates_Operator Keyword_Non Member Functions - Fatal编程技术网

C++ 模板类的非成员比较运算符

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

我定义了一个模板容器树,它有两个成员类迭代器:const_迭代器和iterator

现在我想添加非成员比较运算符:

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操作符==。