Home > Back-end >  when passing std::allocator<type>::pointer to my own wrap_iter i'm getting **type instead
when passing std::allocator<type>::pointer to my own wrap_iter i'm getting **type instead

Time:05-28

I'm trying to create a vector class and my own wrap_iter class

here is my vector.hpp

#ifndef VECTOR_HPP
#define VECTOR_HPP

namespace ft
{
    template <class _Iter>
    struct iterator_traits
    {
    };
    template <class T>
    struct iterator_traits<T*>
    {
        typedef T* pointer;
    };
    template <class T>
    class wrap_iter
    {
    public:
        typedef T iter_type;
        typedef typename ft::iterator_traits<iter_type>::pointer pointer;
    private:
        pointer ptr;
        unsigned int sz;
        int index;
    public:
        wrap_iter(pointer arr, unsigned int sz, int begin): ptr(arr), sz(sz)
        {
            if (begin)
                this->index = 0;
            else
                this->index = sz;
        }
        ~wrap_iter() {}
        T &operator*() const
        {
            if (this->index == (int)this->sz)
                throw std::out_of_range("Trying to access wrong index");
            return (ptr[this->index]);
        }
    };
    template <class T, class A = std::allocator<T> >
    class vector
    {
    public:
        typedef typename A::size_type size_type;
        typedef typename A::pointer pointer;
        typedef ft::wrap_iter<pointer> iterator;
    private:
        pointer arr;
        size_type capacity;
        size_type sz;
    public:
        vector() : arr(new T[1]), capacity(1), sz(0)
        {
        }
        vector(iterator begin, iterator end)
        {
            for (iterator it = begin; it != end;   it)
            {
                if (this->sz == this->capacity)
                {
                    this->reserve(2 * this->capacity);
                }
                this->arr[sz] = *it;
                sz  ;
            }
        }
        void    push_back(T element)
        {
            if (this->sz == this->capacity)
            {
                this->reserve(2 * this->capacity);
            }
            this->arr[sz] = element;
            sz  ;
        }
        void    reserve(size_type am)
        {
            if (am > this->capacity)
            {
                T *temp = new T[am];
                for (size_type i = 0; i < this->capacity;   i)
                    temp[i] = this->arr[i];
                delete[] this->arr;
                capacity = am;
                this->arr = temp;
            }
        }
        iterator begin()
        {
            return (iterator(this->arr, this->sz, 1));
        }
        iterator end()
        {
            return (iterator(this->arr, this->sz, 0));
        }
    };
}

#endif

and here is my main.cpp

#include <iostream>
#include "vector.hpp"
#include <vector>

int main()
{
    ft::vector<int> vec;
    vec.push_back(5);
    vec.push_back(10);
    vec.push_back(15);
    vec.push_back(14);
    ft::vector<int>::iterator it = vec.begin();
    std::cout << *it << std::endl;
}

i'm getting this error when trying to compile

./vector.hpp:40:11: error: non-const lvalue reference to type 'int *' cannot
      bind to a value of unrelated type 'int'
                        return (ptr[this->index]);

So why is the member variable ptr an int ** instead of int*. I'm passing the pointer as a template to wrap_iter. Even when I change pointer ptr; to T ptr; and the parameter pointer arr to T arr I still get the same error. Even when I change typedef typename A::pointer pointer; to typedef T* pointer I get the same error.

CodePudding user response:

The problem is that your overloaded operator* returns an int*(for T=int*) by reference but the actual argument ptr[this->index] that you're returning is of type int and thus the compiler gives the error:

error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
   41 |             return (ptr[this->index]);
      |                    ~~~~^~~~~~~~~~~~~~
      |                        |
      |                        int

why is the member variable ptr an int ** instead of int*.

ptr is an int* and not int** as opposed to your claim.

  • Related