Error compiling passing const

2

There is an error that does not stop to compile and I do not understand, I explain, I pretend to make the power of a language, eg given a language L {0,1}, do L ^ 2, that would give us { 00,01,10,11}.

That's what I intend to do in my code, however, the next one comes out and

passing 'const std::basic_string<char>' as 'this' argument of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' discards qualifiers [-fpermissive]

referring to the line of my code:

(*it1)=iter;

This is the code:

void operaciones::potencia(int exponente){
    set<string>::iterator it1;
    set<string> result;
    char it2;
    char it3;
    int i;
    int j;
    int contador=0;
    while(contador<exponente){

        for(it1=lenguaje.begin(); it1!=lenguaje.end(); it1++){
            for(i=(*it1)[i]; i<(*it1).size();i++){
                for(j=(*it1)[j]; j<(*it1).size();j++){
                    char k=(*it1)[j];
                    string iter=(*it1);
                    iter.push_back(k);
                    (*it1)=iter;
                }
                result.insert((*it1));
            }

        }
        contador++;
    }
    write(result,cout);


}

Class builders

operaciones::operaciones(const string cadena){
        string h; //si, solo se usa en esta funcion, se declara en esta funcion
        int i=0; //en set son desde 0
        //en un lenguaje no puede haber dos cadenas repetidas, es redundante tener mas de una
        int longitud=cadena.length();

        // un while de entrada no sirve porque si encuentra una coma no vuelve  a entrar 

        for (int i=0; i<longitud;i++){ //es caracter por caracter

            while(cadena[i]!='{' && cadena[i]!= ',' && cadena[i]!= '}' && i<longitud){ //las comparaciones con comilla simple, caracteres con comilla simple, string con una doble!!! 
                h.push_back(cadena[i]);
                i++;
            }
            if (cadena[i]==',' || cadena[i]=='}' ){

                lenguaje.insert(h); //contenido de string
                h.clear();
            }


        }


    }

    operaciones::operaciones(const operaciones& lenguaje3){//constructor de copia
        lenguaje=lenguaje3.lenguaje;//accediendo atributo lenguaje3
    }

Really, I thank you for explaining where you see the bug and how to correct it

Thanks

    
asked by AER 04.10.2017 в 02:27
source

1 answer

2

One thing I do not understand: what do you intend to achieve with this?

result.insert((*it1))

You keep the string unchanged in your result . Should not you do

result.insert( iter );

Now, to the topic :

There is a little detail about the containers set that you have escaped: once you have entered a value in them, can not be modified .

The explanation is very simple: internally, these containers have a certain internal structure, only known by them. Normally, they are implemented with a tree , to allow fast binary searches.

In that image, imagine that you have an iterator pointing to the 6 element, and you change it to 15 . What will happen?

That the tree is broken . Its internal structure is no longer valid, and any attempt to search for any data will be undefined behavior ; most likely the program will crash, or try to access memory that does not belong to you, or fence you to know .

Therefore, the iterators provided by a set do not allow modifying the data they point to ; they behave, for all purposes, as if they were const_iterator (in fact, it is the data what is const ).

That's what the message is telling you: *iter points to a const string , and the latter does not support operator= . It's a string read-only .

The solution is to create a new set and to store the modified elements in it; which you already do in result . At the end of the function, just assign your result to your lenguaje .

void operaciones::potencia(int exponente){
  set<string>::iterator it1;
  set<string> result;
  char it2;
  char it3;
  int i;
  int j;
  int contador=0;

  while(contador<exponente){    
    for(it1=lenguaje.begin(); it1!=lenguaje.end(); it1++){
      for(i=(*it1)[i]; i<(*it1).size();i++){
         for(j=(*it1)[j]; j<(*it1).size();j++){
            char k=(*it1)[j];
            string iter=(*it1);
            iter.push_back(k);

            // (*it1)=iter;
          }
        result.insert(iter); // <- ¿¿ no será esto lo que quieres ??
      }  
    }
    contador++;
  }
  lenguaje = result; // <- Añadimos esto
  write(result,cout);
}

If my assumptions about what you're trying are correct, that should suffice.

    
answered by 04.10.2017 / 05:52
source