简体   繁体   English

为什么我的模板化函数需要从一个迭代器到另一个迭代器的转换?

[英]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_MyIteratorMyIterator转换。

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.

相关问题 为什么转换 function 声明不需要至少一个定义类型说明符 - why a conversion function declaration does not require at least one defining-type-specifier 为什么我的函数超载不是我的模板优先? - Why is my function overload not preferred over my templated one? 为什么隐式转换不适用于模板化的 function 参数? - Why is implicit conversion not applied to templated function parameter? 为什么我的 DLL 不需要 DllMain function? - Why does my DLL not require a DllMain function? 如何编写以通用迭代器为参数的模板化 function - How can one write a templated function that takes a generic iterator as a parameter 为什么BOOST_TEST((Iterator == Iterator))需要额外的括号? - Why does BOOST_TEST((Iterator == Iterator)) require extra parentheses? 为什么 std::sort 不要求用户指定模板类型? - why does std::sort not require the user to specify the templated types? 为什么我的自定义迭代器需要基于范围的for循环中的调用运算符? - Why does my custom iterator require a call operator in range based for loops? C++ 隐式转换规则为什么以及如何区分模板转换函数和非模板转换函数? - Why and how do the C++ implicit conversion rules distinguish templated conversion functions from non-templated? 为什么我的模板运算符==不被使用? - Why does my templated operator== not get used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM