there is an error that keeps coming out when compiling and I don't understand it, I'll explain, I intend to do the power of a language, eg given a language L {0,1}, do L^2, that would give us {00,01 ,10,11}.
that is what I intend to do in my code, however, the following 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 constructors
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 appreciate you explaining to me where you see the error and how to correct it
Thank you
One thing I don't understand: what are you trying to achieve with this?
You save the unmodified string in your
result
. shouldn't you doNow, to the topic :
There's a little detail about containers
set
that you missed: once a value is entered into a container, it cannot be changed .The explanation is very simple: internally, those containers have a certain internal structure, only known to them. They are typically implemented with a tree , to allow for fast binary searches .
In that image, imagine you have an iterator pointing to the element
6
, and you change it to a15
. What will happen ?That the tree breaks . Its internal structure is no longer valid, and any attempt to search for some data will be undefined behavior ; the most probable thing is that the program crashes, or tries to access memory that does not belong to it, or who knows what .
Thus, the iterators provided by a
set
do not allow the data they point to to be modified ; they behave, to all intents and purposes, as if they wereconst_iterator
(actually, it is the data that isconst
).That's what the message is telling you: it
*iter
points to aconst string
, and the latter doesn't support theoperator=
. It is astring
read-only .The solution is to create a new one
set
and store the modified elements in it; which you already do inresult
. At the end of the function, just assign turesult
to tulenguaje
.If my assumptions about what you're trying are correct, that should do the trick.