0

I wrote my own matrix class with fields like this

    template <typename T>
        class Matrix
        {
        private:
            T *data = nullptr;
            size_t rows;
            size_t cols;
....

I tried to make an iterator for my matrix, but it didn't work. An error like this is thrown in method Iterator begin(and end):

error: cannot convert ‘myMatrix::Matrix<int>::Iterator’ to ‘int*’ in return

The iterator must support STL functions such as std::find_if(). How can I fix the iterator so that it works correctly?

 class Iterator
        {
            friend Matrix;

        private:
            T *curr;

        public:

            using iterator_category = std::input_iterator_tag;
            using value_type = T;
            using difference_type = std::ptrdiff_t;
            using pointer = value_type *;
            using reference = value_type &;

            Iterator() : curr(nullptr) {}

            Iterator(T *other) : curr(other) {}

            ~Iterator() = default;

            bool operator==(const Iterator &it) const { return curr == it.curr; }
            bool operator!=(const Iterator &it) const { return !(curr == it.curr); }

            Iterator &operator++()
            {
                ++curr;
                return *this;
            }

            Iterator operator++(int)
            {
                Iterator temp = *this;
                operator++();
                return temp;
            }

            Iterator &operator+(int n)
            {
                for (int i = 0; i < n; i++)
                {
                    ++(*this);
                }
                return *this;
            }

            T &operator*() const { return *curr; }

            T *operator->() const { return curr; }
        };

        Iterator begin()
        {
            Iterator it(data);
            return it;
        }

        Iterator end()
        {
            Iterator it(data + rows * cols);
            return it;
        }
  • You could simply implement `begin()` -> return `data`, and `end()` -> return `data + rows*cols`. A simple pointer is a perfect iterator for an array. You don't need that `Iterator` class. For the error: I guess you have a function in Matrix like: `T* begin() { return Iterator::begin();}` And you can't return an `Iterator` as a `T*` – simre May 27 '22 at 20:30
  • I understand that I can implement just returning the beginning and end of the matrix, but according to the assignment, I am obliged to implement an iterator that supports STL functions (find and others). Here is an example: https://godbolt.org/z/qnq4rMavb – Елизавета Тараненко May 27 '22 at 20:36
  • 1
    @ЕлизаветаТараненко Oh okay so this is an assignment. But your example works for me. Just write `std::cout << *res << std::endl;` at the end. You have to dereference that iterator or overload `ostream<<` or something – simre May 27 '22 at 20:39
  • True, I just compiled on version 17. So if my iterator works with std::find, then it works with STL? But for some reason it doesn't work with std::sort, there's an error somewhere – Елизавета Тараненко May 27 '22 at 20:44
  • @ЕлизаветаТараненко It should. But you should add `#include ` for the `std::find` overload before C++20 as I see... For "STL iterators" check this question, maybe it can help with the details: https://stackoverflow.com/questions/8054273/how-to-implement-an-stl-style-iterator-and-avoid-common-pitfalls – simre May 27 '22 at 20:50

0 Answers0