[英]Why does my templated function require a conversion from one iterator to the other?
I am trying to implement a binary search tree container.我正在尝试实现一个二叉搜索树容器。 At the moment I have to implement a find() function that is able to return an iterator or a constant iterator.
目前我必须实现一个能够返回迭代器或常量迭代器的 find() 函数。 I choose to overload the find function to accomodate both possibilities
我选择重载 find 函数以适应这两种可能性
MyIterator<treepair> find(const key& x)
{
return tree_search<MyIterator<treepair>>(root,x);
}
const_MyIterator<treepair> find(const key& x) const
{
return tree_search<const_MyIterator<treepair>>(root,x);
}
Then the function tree_search recursively finds the node that contains the wanted key by percurring the tree:然后函数 tree_search 通过遍历树递归地找到包含所需键的节点:
template<typename iterator>
iterator tree_search(Node<treepair>* x, const key& y) const
{
if(x == nullptr)
{
std::cout<<"element not found"<<std::endl;
iterator x = end();//HERE I HAVE A PROBLEM
return x;
}
else if (y == x->value.first)
{
iterator i{x,tree_maximum()};
std::cout<<"element found"<<std::endl;
return i;
}
if(y < x->value.first)
return tree_search<iterator>(x->left,y);
else return tree_search<iterator>(x->right,y);
}
Now the end() function is overloaded to give both a const_iterator and a regular iterator:现在 end() 函数被重载以提供一个 const_iterator 和一个常规迭代器:
MyIterator<treepair> end(){
return MyIterator<treepair>{nullptr,tree_maximum()};
}
const_MyIterator<treepair> end() const{
return const_MyIterator<treepair>{nullptr,tree_maximum()};
}
however I receive this error但是我收到这个错误
test_on_iterators.cc:508:12: error: conversion from ‘const_MyIterator<std::pair<int, int> >’ to non-scalar type ‘MyIterator<std::pair<int, int> >’ requested
iterator x = end();
Is this error due to a requirement in the conversion between the types?这个错误是由于类型之间的转换需要吗? Isn't the compiler supposed to choose the wanted end() function according to the iterator type it has to produce?
编译器不应该根据它必须生成的迭代器类型来选择想要的 end() 函数吗?
Isn't the compiler supposed to choose the wanted end() function according to the iterator type it has to produce?
编译器不应该根据它必须生成的迭代器类型来选择想要的 end() 函数吗?
No.不。
tree_search()
is a const
method. tree_search()
是一个const
方法。 That means its this
pointer is pointing at a const
object (even if the object tree_search()
is called on is not really const
).这意味着它的
this
指针指向一个const
对象(即使调用对象tree_search()
并不是真正的const
)。
As such, when tree_search()
internally calls end()
, it calls the overload that is callable on a const
object.因此,当
tree_search()
内部调用end()
,它会调用可在const
对象上调用的重载。 That overload returns a const_MyIterator
.该重载返回一个
const_MyIterator
。 Then tree_search()
tries to assign that const_MyIterator
to a non-const MyIterator
, which is where you get the error since there is no conversion defined from const_MyIterator
to MyIterator
.然后
tree_search()
尝试将该const_MyIterator
分配给非常量MyIterator
,这是您收到错误的地方,因为没有定义从const_MyIterator
到MyIterator
转换。
You need to make x
be a const_MyIterator
to match what the const
version of end()
returns.您需要使
x
成为const_MyIterator
以匹配end()
的const
版本返回的内容。
You should also make tree_search()
return a const_MyIterator
as well, instead of returning a non-const iterator
.您还应该让
tree_search()
返回一个const_MyIterator
,而不是返回一个非常量iterator
。 If you want tree_search()
to return a non-const Iterator
, then don't declare it as a const
method.如果您希望
tree_search()
返回非常量Iterator
,则不要将tree_search()
声明为const
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.