简体   繁体   English

自定义数组迭代器的取消引用运算符

[英]Dereferencing operator for custom array iterator

I am practicing implementing a random access iterator for a custom class:我正在练习为自定义 class 实现随机访问迭代器:

 template<typename T>
    class Array{
    friend class Iterator;
    private:
        std::unique_ptr<T[]> data;
        int front_index;
        //other variables
    public:
        class Iterator{
            friend class Array<T>;
        private:
            int position;
        public:
            Iterator(int pos) {position = pos; }
            //some other iterator functions
            template<typename T>
            T operator*(){
                return data[position];
            }
        };
        Iterator begin() const { return Iterator(front_index); }
        //other Array functions(insert, delete)
    };

But when I call this code:但是当我调用这段代码时:

Array<int> a;
//insert some elements
Array<int>::Iterator iter1 = a.begin();
cout << "iterator is " << *iter1 << endl; 

It gives the following error:它给出了以下错误:

C++ no operator matches these operands, operand types are: * Array<int>::Iterator 

It seems like the error is coming from the return data[position];似乎错误来自return data[position]; line.线。 If I only write return position that the code runs but this is not what I want (I want that when I dereference the ierator returns the element in the specific position. Any inputs are appreciated!如果我只写代码运行的return position但这不是我想要的(我希望当我取消引用时 ierator 返回特定 position 中的元素。任何输入都表示赞赏!

It gives the following error:它给出了以下错误:

 C++ no operator matches these operands, operand types are: * Array<int>::Iterator

It seems like the error is coming from the return data[position];似乎错误来自返回数据[位置]; line.线。 If I only write return position that the code runs but this is not what I want (I want that when I dereference the ierator returns the element in the specific position.如果我只写代码运行的 return position 但这不是我想要的(我希望当我取消引用时 ierator 返回特定 position 中的元素。

It shouldn't, since that does nothing to solve this bug.它不应该,因为这对解决这个错误没有任何帮助。 It's unclear why you would think otherwise.目前还不清楚你为什么会这么想。


Your example has three fundamental problems.你的例子有三个基本问题。 One is that you have nested template, where the inner template parameter shadows outer template parameter.一种是您有嵌套模板,其中内部模板参数隐藏了外部模板参数。 This is not allowed in C++, resulting in the following error: C++中不允许这样做,导致如下错误:

error: declaration of template parameter 'T' shadows template parameter

Simple solution is to use another name for any nested template.简单的解决方案是为任何嵌套模板使用另一个名称。

Another problem is the one that you quote.另一个问题是您引用的问题。 Reading the error message further leads us to the solution:阅读错误消息进一步引导我们找到解决方案:

 error: no match for 'operator*' (operand type is 'Array<int>::Iterator')
   35 |     cout << "iterator is " << *iter1 << endl;
      |                               ^~~~~~
<source>:19:15: note: candidate: 'template<class T> T Array<T>::Iterator::operator*() [with T = T; T = int]'
   19 |             T operator*(){
      |               ^~~~~~~~
<source>:19:15: note:   template argument deduction/substitution failed:
<source>:35:32: note:   couldn't deduce template parameter 'T'
   35 |     cout << "iterator is " << *iter1 << endl;

The compiler cannot know what template parameter to use, since it cannot be deduced from arguments, and you didn't specify any.编译器不知道要使用什么模板参数,因为它不能从 arguments 推导出来,而且您没有指定任何参数。 Technically, this could be solved by specifying the argument explicitly:从技术上讲,这可以通过明确指定参数来解决:

<< iter1.operator*<int>()

However, I question why is the indirection operator a template in the first place?但是,我质疑为什么间接运算符首先是模板? Why not always return T of the outer template?为什么不总是返回外部模板的T I propose to make it a regular function:我建议将其设为常规 function:

// not a template
T   // this is the outer template parameter
operator*(){

The third problem can be seen after these fixes:在这些修复之后可以看到第三个问题:

error: invalid use of non-static data member 'Array<int>::data'
   19 |                 return data[position];

This is the bug that your suggested change "solves" - in the sense that the program becomes well-formed.这是您建议的更改“解决”的错误 - 从某种意义上说,程序变得格式良好。 It just doesn't do anything meaninful.它只是没有做任何有意义的事情。

The problem of course is that the iterator doesn't have a member data .问题当然是迭代器没有成员data A typical solution is to store a non-owning pointer to element of the array, rather than just a position which cannot by itself be used to find elements of an array:一个典型的解决方案是存储一个指向数组元素的非拥有指针,而不仅仅是一个 position,它本身不能用于查找数组的元素:

T* data;

T operator*(){
    return *data;

In fact since pointer is an iterator for array, if you were lazy, instead of defining a custom class you could simply do:事实上,由于指针是数组的迭代器,如果你很懒惰,而不是定义自定义 class 你可以简单地做:

using Iterator = int*;

That said, a custom class lets you catch some bugs at compile time, so it's a good design.也就是说,自定义 class 可以让您在编译时捕获一些错误,所以这是一个很好的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM