自定义容器列表:从迭代器编译为常量迭代器时出现问题 我试图为学校项目重新创建一些C++容器,为此我还必须实现迭代器。我目前正在处理列表容器,并且面临转换问题。 以下是涉及的代码部分: 我有一个Elem结构(对应于我用于列表容器的双链接列表的1个元素) 模板 结构元素 { 元素*prev; T数据; Elem*下一步; }; 双向迭代器类(用于列表迭代器)。以下是施工人员: 模板 类双向运算符 { 公众: typedef双向迭代器; 类型定义T值_类型; typedef size_t size_type; 双向运算符(){u ptr=nullptr;}; 双向运算符(Elem*ptr){ *这个->\u ptr=ptr; }; 双向迭代器(常量迭代器&x){ *这个->\u ptr=x.\u ptr; }; ~BidirectionalIterator(){}; 迭代器和运算符=(常量迭代器和x){ *这个->\u ptr=x.\u ptr; 返回(*本条); }; [...] }; 和我的列表类: 模板 班级名单 { 公众: 类型定义T值_类型; typedef双向迭代器; typedef双向迭代器常量迭代器; typedef size_t size_type; /*建设者*/ [...] 列表(常数列表和x){ _init_list(); 赋值(x.begin(),x.end()); }; /*迭代器*/ 迭代器begin(){ 返回(迭代器(_start)); }; 常量迭代器begin()常量{ 返回(常量迭代器(_开始)); }; 迭代器结束(){ 返回(迭代器(_tail)); }; 常量迭代器end()常量{ 返回(常量迭代器(_尾)); }; /*分配*/ void赋值(迭代器优先,迭代器最后); void assign(const_iterator first,const_iterator last); [...] 私人: 电子头; 元素*_开始; 元素*_结束; 元素尾; [...] };

自定义容器列表:从迭代器编译为常量迭代器时出现问题 我试图为学校项目重新创建一些C++容器,为此我还必须实现迭代器。我目前正在处理列表容器,并且面临转换问题。 以下是涉及的代码部分: 我有一个Elem结构(对应于我用于列表容器的双链接列表的1个元素) 模板 结构元素 { 元素*prev; T数据; Elem*下一步; }; 双向迭代器类(用于列表迭代器)。以下是施工人员: 模板 类双向运算符 { 公众: typedef双向迭代器; 类型定义T值_类型; typedef size_t size_type; 双向运算符(){u ptr=nullptr;}; 双向运算符(Elem*ptr){ *这个->\u ptr=ptr; }; 双向迭代器(常量迭代器&x){ *这个->\u ptr=x.\u ptr; }; ~BidirectionalIterator(){}; 迭代器和运算符=(常量迭代器和x){ *这个->\u ptr=x.\u ptr; 返回(*本条); }; [...] }; 和我的列表类: 模板 班级名单 { 公众: 类型定义T值_类型; typedef双向迭代器; typedef双向迭代器常量迭代器; typedef size_t size_type; /*建设者*/ [...] 列表(常数列表和x){ _init_list(); 赋值(x.begin(),x.end()); }; /*迭代器*/ 迭代器begin(){ 返回(迭代器(_start)); }; 常量迭代器begin()常量{ 返回(常量迭代器(_开始)); }; 迭代器结束(){ 返回(迭代器(_tail)); }; 常量迭代器end()常量{ 返回(常量迭代器(_尾)); }; /*分配*/ void赋值(迭代器优先,迭代器最后); void assign(const_iterator first,const_iterator last); [...] 私人: 电子头; 元素*_开始; 元素*_结束; 元素尾; [...] };,c++,iterator,containers,C++,Iterator,Containers,在我的主程序中,我只是调用一个隐式调用复制构造函数的函数(T是int): 无效打印内容(ft::list lst) 但当我编译时,我得到了: ./List.hpp:71:12: error: no matching conversion for functional-style cast from 'Elem<ft::list<int, std::allocator<int> >::value_type> *const' (aka 'Elem<

在我的主程序中,我只是调用一个隐式调用复制构造函数的函数(T是int):

无效打印内容(ft::list lst)

但当我编译时,我得到了:

./List.hpp:71:12: error: no matching conversion for functional-style cast from 'Elem<ft::list<int, std::allocator<int>
      >::value_type> *const' (aka 'Elem<int> *const') to 'ft::list<int, std::allocator<int> >::const_iterator' (aka
      'BidirectionalIterator<const int>')
                        return (const_iterator(_start));
                                ^~~~~~~~~~~~~~~~~~~~~
./List.hpp:53:13: note: in instantiation of member function 'ft::list<int, std::allocator<int> >::begin' requested
      here
                        assign(x.begin(), x.end());
./../Iterator/BidirectionalIterator.hpp:45:3: note: candidate constructor not viable: no known conversion from
      'Elem<ft::list<int, std::allocator<int> >::value_type> *const' (aka 'Elem<int> *const') to
      'Elem<ft::BidirectionalIterator<const int>::value_type> *' (aka 'Elem<const int> *') for 1st argument
                BidirectionalIterator(Elem<value_type> *ptr) {
/List.hpp:71:12:错误:函数样式从'Elem*const'(又名'Elem*const')转换为'ft::List::const_iterator'(又名'Elem*const')没有匹配的转换
“双向运算符”)
返回(常量迭代器(_开始));
^~~~~~~~~~~~~~~~~~~~~
./List.hpp:53:13:注意:在成员函数“ft::List::begin”的实例化中请求
在这里
赋值(x.begin(),x.end());
../../Iterator/BidirectionalIterator.hpp:45:3:注意:候选构造函数不可行:没有已知的从
“Elem*const”(又名“Elem*const”)至
第一个参数为'Elem*'(又称'Elem*')
双向运算符(Elem*ptr){
我不知道如何解决这个问题。我已经尝试从我的复制构造函数中删除const属性,它可以工作,但它需要是const(对于我的项目的其余部分,因为我正在实现调用const列表的关系运算符,同时也尊重原始容器构造函数)


有人有想法吗?

您尝试从
元素*常量创建
元素*

我建议使用迭代器的指针
Elem*
(即使对于
const\u迭代器
),但让取消引用
const\u迭代器
返回
T const&
T const*

例如:

模板
类双向运算符{
公众:
使用值_type=T;
使用引用=值\类型&;
使用指针=值_类型*;
使用size\u type=std::size\u t;
双向运算符():ptr(nullptr){};
双向运算符(Elem*ptr):\u ptr(ptr){};
双向运算符(常量双向运算符&x){
_ptr=x.\u ptr;
};
双向运算符和运算符=(常量双向运算符和x){
_ptr=x.\u ptr;
归还*这个;
};
引用运算符*()常量{return\u ptr->data;}
指针运算符->()常量{return&_ptr->data;}
元素*\u ptr;
};
稍微好一点的版本可以让您创建
常量T
s的
列表
s,还可以让您将
迭代器
s转换为
常量迭代器
s(但不是相反),以便能够比较迭代器,可以如下所示:

#包括
#包括
模板//常量或非常量以及元素中使用的类型
类双向运算符{
公众:
使用值_type=T;
使用引用=值\类型&;
使用指针=值_类型*;
使用size\u type=std::size\u t;
双向运算符():ptr(nullptr){};
双向运算符(Elem*ptr):\u ptr(ptr){};
//让常量迭代器的转换构造函数读取
友元类双向算子;
//允许通过从非常量迭代器创建常量迭代器
//转换构造函数
模板
双向运算符(常量双向运算符&x):
_ptr(x.ptr){}
//正常复制选择器
双向运算符(常量双向运算符&x):\u ptr(x._ptr){}
双向运算符和运算符=(常量双向运算符和x){
_ptr=x.\u ptr;
归还*这个;
};
//转换构造函数允许您比较常量迭代器和迭代器
布尔运算符==(常量双向运算符&rhs)常量{
返回_ptr==rhs._ptr;
}
布尔运算符!=(常量双向运算符和rhs)常量{
返回!(\u ptr==rhs.\u ptr);
}
引用运算符*()常量{return\u ptr->data;}
指针运算符->()常量{return&_ptr->data;}
私人:
元素*\u ptr;
};
//迭代器==常数
template <class T>
class BidirectionalIterator
{
  public:
  typedef BidirectionalIterator iterator;
  typedef T value_type;
  typedef size_t size_type;

  BidirectionalIterator() { _ptr = nullptr; };
  BidirectionalIterator(Elem<value_type> *ptr) {
     *this->_ptr = ptr;
  };
  BidirectionalIterator(const iterator &x) {
     *this->_ptr = x._ptr;
  };
  ~BidirectionalIterator() {};
  iterator    &operator=(const iterator &x) {
     *this->_ptr = x._ptr; 
     return (*this);
  };

  [...]
};
template <class T, class Alloc = std::allocator<T>>
  class list
  {
      public:
      typedef T value_type;
      typedef BidirectionalIterator<T> iterator;
      typedef BidirectionalIterator<const T> const_iterator;
      typedef size_t size_type;

      /* CONSTRUCTORS */
      [...]
      list(const list &x) {
          _init_list();
          assign(x.begin(), x.end());
      };

      /* ITERATORS */
      iterator    begin() {
          return (iterator(_start));
      };
      const_iterator  begin() const {         
          return (const_iterator(_start));
      };
      iterator        end() {
          return (iterator(_tail));
      };
      const_iterator  end() const {           
          return (const_iterator(_tail));
      };

      /* ASSIGN */
      void assign(iterator first, iterator last);
      void assign(const_iterator first, const_iterator last);

      [...]

      private:
      Elem<value_type>    *_head;
      Elem<value_type>    *_start;
      Elem<value_type>    *_end;
      Elem<value_type>    *_tail;
      [...]
  };
./List.hpp:71:12: error: no matching conversion for functional-style cast from 'Elem<ft::list<int, std::allocator<int>
      >::value_type> *const' (aka 'Elem<int> *const') to 'ft::list<int, std::allocator<int> >::const_iterator' (aka
      'BidirectionalIterator<const int>')
                        return (const_iterator(_start));
                                ^~~~~~~~~~~~~~~~~~~~~
./List.hpp:53:13: note: in instantiation of member function 'ft::list<int, std::allocator<int> >::begin' requested
      here
                        assign(x.begin(), x.end());
./../Iterator/BidirectionalIterator.hpp:45:3: note: candidate constructor not viable: no known conversion from
      'Elem<ft::list<int, std::allocator<int> >::value_type> *const' (aka 'Elem<int> *const') to
      'Elem<ft::BidirectionalIterator<const int>::value_type> *' (aka 'Elem<const int> *') for 1st argument
                BidirectionalIterator(Elem<value_type> *ptr) {