将 std::allocator<type>::pointer 传递给我自己的 wrap_iter 时,我得到的是 **type 而不是 *type
when passing std::allocator<type>::pointer to my own wrap_iter i'm getting **type instead of *type
我正在尝试创建一个矢量 class 和我自己的 wrap_iter class
这是我的 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
这是我的 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;
}
我在尝试编译时遇到此错误
./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]);
那么为什么成员变量ptr是int**而不是int*呢。我将指针作为模板传递给 wrap_iter。即使我将 pointer ptr;
更改为 T ptr;
并将参数 pointer arr
更改为 T arr
我仍然会遇到相同的错误。即使我将 typedef typename A::pointer pointer;
更改为 typedef T* pointer
,我也会遇到同样的错误。
问题 是你的重载 operator*
returns 一个 int*
(for T=int*
) 但实际您返回的参数 ptr[this->index]
是 int
类型,因此编译器给出 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
是 int*
而不是 int**
与您的说法相反。
我正在尝试创建一个矢量 class 和我自己的 wrap_iter class
这是我的 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
这是我的 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;
}
我在尝试编译时遇到此错误
./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]);
那么为什么成员变量ptr是int**而不是int*呢。我将指针作为模板传递给 wrap_iter。即使我将 pointer ptr;
更改为 T ptr;
并将参数 pointer arr
更改为 T arr
我仍然会遇到相同的错误。即使我将 typedef typename A::pointer pointer;
更改为 typedef T* pointer
,我也会遇到同样的错误。
问题 是你的重载 operator*
returns 一个 int*
(for T=int*
) 但实际您返回的参数 ptr[this->index]
是 int
类型,因此编译器给出 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
是 int*
而不是 int**
与您的说法相反。