When executing a program in which, among other functions, I minimize a deterministic finite automaton (DFA, for more information, https://es.wikipedia.org/wiki/Aut%C3%B3mata_deterministic_finite ), I use the set and map containers of the stl from C++, but it gives me errors that I don't understand in that function (dfaminimo), the errors are the following:
I appreciate your help.
estadoDFA.cpp:34:12: error: binding ‘const std::map<char, int>’ to reference of type ‘std::map<char, int>&’ discards qualifiers
return trans_;
referring to the following line of the EstadoDFA file:
map<char,int>& estado:: get_transiciones(void) const{
return trans_; //LINEA 34
}
In that function I have made countless patches, putting that what it returns is always const
, putting that it const
goes after get_transiciones(void)
, but it gives me a lot of more errors.
the second mistake:
map& state:: get_transitions(void) const{ return trans_; }
PS: I have looked at similar questions with the same error, trying the proposed solutions, but it does not compile for me.
DFA.cpp:414:74: error: 'class std::map' has no member named 'second' if(oldpart[k].find((*it).get_transitions().second) == oldpart[k] .end()){
I don't understand this error either.
Here I leave the code of the functions with error:
the one of dfaminimo in DFA.cpp
void dfa::dfaminimo(){
//cjtos.push_back(cjto);
//un vector al que le inserto cosas parametro
map<char,int>::iterator it1;
//vector<set<int> > cjtos;
set<estado> particion1; //el estado de muerte
set<estado> particion2; //el resto de estados
vector<set<estado> > conjuntos;
//no hace falta llamar a la funcion operator <, lo llama insertar en su implementacion interna
//particion2=cjtos;
for(int i=0;i<estados_.size();i++){
if(estados_[i].get_aceptacion()!=true){
particion2.insert(estados_[i]);
}
else{
particion1.insert(estados_[i]);
}
}
conjuntos.push_back(particion1);
conjuntos.push_back(particion2);
vector<set<estado> > temporal;
do{
temporal = conjuntos;
conjuntos = crear_nueva_particion(temporal);
}while(conjuntos.size()!=temporal.size());
construir_dfa(conjuntos);
}
Here the functions that are called:
vector<set<estado> > dfa::crear_nueva_particion(vector<set<estado> > temporal){
vector<set<estado> > conjunto;
for(int i=0; i<temporal.size();i++){
//conjunto = conjunto | descomp(temporal[i],temporal);
set<estado> newpart = descomp(temporal[i],temporal);
//set<estado>::iterator it1;
conjunto.push_back(newpart);
}
//unione.push_back(operando2);
return conjunto;
//return unione;
}
Here the decompose function, where I separate the sets
set<estado> dfa::descomp(set<estado> conj, vector<set<estado> > partvieja){
set<estado> T = conj;
map<char, int>::iterator it1;
set<char> simbolos;//simbolos del alfabeto que el DFA reconoce
for(it1=estados_[0].get_transiciones().begin();it1!=estados_[0].get_transiciones().end();it1++){
simbolos.insert((*it1).first);
}
set<char>::iterator it2;
for(it2=simbolos.begin(); it2!=simbolos.end();it2++){
set<estado> P;
set<estado>::iterator iterador;
for(iterador= T.begin(); iterador!=T.end();iterador++){
set<estado> Tprima = part(conj,(*it2),partvieja);
//P = P | Tprima;
set<estado>::iterator it1;
for(it1= Tprima.begin(); it1!=Tprima.end();it1++){
P.insert((*it1));
}
}
T = P;
}
return T;
}
Thank you
The error is clearly telling you what the problem is, maybe you don't understand it because it is in English, let me translate it:
It's basically telling you that an object of type constant cannot be referenced by a non-constant reference. In C++ it is illegal to have a non-constant reference to a constant object.
The object
trans_
you return in the functionget_transiciones
is constant because the function is constant:Solution.
You can fix this problem in two ways: by making the constant return or by removing the function qualifier:
Or without constant: