简体   繁体   English

C ++,使用const_iterator进行运算符=时遇到问题

[英]C++, having issues using const_iterator to make operator=

There is a compilation error which occurs when I write the following: 当我编写以下代码时,会发生编译错误:

const_iterator it = cp.begin();

const_iterator is my own class for const iterator . const_iterator是我自己的const iterator类。
cp is an object of a class ConjuntoPreguntas (see below). cpConjuntoPreguntas类的对象(请参见下文)。
The error is: 错误是:

mainprueba.cpp:30:6: error: no match for ‘operator=’ (operand types are ‘ConjuntoPreguntas::const_iterator’ and ‘ConjuntoPreguntas::iterator’)
  cit = CP.begin();
      ^
mainprueba.cpp:30:6: note: candidate is:
In file included from mainprueba.cpp:2:0:
conjuntopreguntas.h:258:21: note: ConjuntoPreguntas::const_iterator& ConjuntoPreguntas::const_iterator::operator=(const ConjuntoPreguntas::const_iterator&)
     const_iterator& operator=(const const_iterator& cit){
                     ^
conjuntopreguntas.h:258:21: note:   no known conversion for argument 1 from ‘ConjuntoPreguntas::iterator’ to ‘const ConjuntoPreguntas::const_iterator&’

The code: 编码:

class ConjuntoPreguntas{
private:
    map<int,Pregunta> preguntas;

public:     
    class const_iterator;

    class iterator{
    private:
          map<int,Pregunta>::iterator it;

    public:
          iterator & operator++(){
           ++it;
          }

          iterator & operator--(){
           --it;
          }

          pair<const int,Pregunta> &operator *(){
           return *it;
          }

          bool operator ==(const iterator &i){
           return i.it==it;
          }   

          bool operator !=(const iterator &i){
           return i.it!=it;
          }

          friend class ConjuntoPreguntas;
          friend class const_iterator;
    };


    class const_iterator{
    private:
          map<int,Pregunta>::iterator it;
    public:
          const_iterator(){ 
          }

          const_iterator & operator++(){
           ++it;
          }

          const_iterator & operator--(){
           --it;
          }

          pair<const int,Pregunta> &operator *(){
           return *it;
          }

          bool operator ==(const const_iterator &i){
           return i.it==it;
          }   

          bool operator !=(const const_iterator &i){
           return i.it!=it;
          }

          const_iterator& operator=(const const_iterator& cit){
          }

          friend class ConjuntoPreguntas;

    };

    iterator begin(){
        iterator i;
        i.it=preguntas.begin();
        return i;
    }   

    iterator end(){
        iterator i;
        i.it=preguntas.end();
        return i;
    }
/* other code, irrelevant to the problem */                             
};   

If anyone can help me, I will be very grateful. 如果有人可以帮助我,我将非常感激。

Your most immediate problem is because you don't have a const version of begin : 您最直接的问题是因为您没有beginconst版本:

   const_iterator begin() const
   {
        const_iterator i;
        i.it = preguntas.begin();
        return i;
    }

But then also your const_iterator class uses map's iterator which is going to result in another problem. 但是,然后您的const_iterator类使用map的迭代器,这将导致另一个问题。

If you're writing a container-looking class, there's nothing for it but to write const-correct iterator and const_iterator classes and provide the const-correct members. 如果您正在编写一个看起来像容器的类,那么除了编写const正确的迭代器和const_iterator类并提供const正确的成员之外,别无其他。

If however you are NOT writing a container, you may not want to do this. 但是,如果您没有编写容器,则可能不希望这样做。 The best case is to provide a container-agnostic interface to the class, where for example you provide meaningful names rather than direct container access. 最好的情况是为该类提供一个与容器无关的接口,例如,您在其中提供有意义的名称,而不是直接进行容器访问。 Alternately provide const-only access via the map const_iterator (don't write your own iterator class). 或者通过映射const_iterator提供仅const访问(不要编写您自己的迭代器类)。

If you want to fully encapsulate std container inside your class (daunting and usually unneccessary task) you need to make sure you also define all conversions. 如果要将std容器完全封装在类中(艰巨且通常是不必要的任务),则需要确保还定义了所有转换。 In particlar, standard container iterators have a constructor which creates const_iterator from iterator (but not the other way around!). 特别是,标准容器迭代器具有一个构造函数,该构造函数从迭代器创建const_iterator(但反之则不行!)。 You will have to create it yourselfa as well. 您也必须自己创建它。

However, a much better design choice would be to simply expose the member map variable. 但是,更好的设计选择是仅公开成员map变量。

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

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