error when compiling binding to reference of type discards qualifiers


when executing a program in which among other functions, I minimize a finite deterministic automaton (DFA, for more information, link ), I use the set and map containers of the C ++ stl, but it gives me errors that I do not understand in that function (dfaminimo), the errors are as follows:

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 in the DFA file:

map<char,int>&  estado:: get_transiciones(void) const{
        return trans_; //LINEA 34

in that function I've done an infinite number of patches, putting what it returns is always const , putting the const going after get_transiciones(void) , but it gives me a lot more errors.

the second error:

map & state :: get_transiciones (void) const {         return trans_;     }

PS: I have looked at similar questions with the same error, trying the solutions that are proposed, but it does not compile me.

DFA.cpp: 414: 74: error: 'class std :: map' has no member named 'second'                                 if (partvieja [k] .find ((* it) .get_transiciones (). second) == partvieja [k] .end ()) {

This mistake I do not understand either.

Here I leave the code of the functions with error:

that of dfaminimo in DFA.cpp

void dfa::dfaminimo(){
                        //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
            for(int i=0;i<estados_.size();i++){



            vector<set<estado> > temporal;

                temporal = conjuntos;
                conjuntos = crear_nueva_particion(temporal);



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;



           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

           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++){

               T = P;
           return T;



asked by AER 29.10.2017 в 22:05

1 answer


The error is clearly telling you what the problem is, maybe you do not understand it because it is in English, let me translate it:


error: binding const std::map<char, int> to reference of type std::map<char, int>& discards qualifiers


error: link const std::map<char, int> to a reference of type std::map<char, int>& discard qualifiers

It's basically telling you that an object of constant type can not be referenced by a non-constant reference. In C ++ it is illegal to have a non-constant reference to a constant object.

The trans_ object that you return in the get_transiciones function is constant because the function is constant:

//           /--- referencia
//           v           funcion constante ---> vvvvv
map<char,int>&  estado:: get_transiciones(void) const{
    return trans_;
//         ^^^^^^ <--- es constante porque get_transiciones es constante


You can solve this problem in two ways: by making the return constant or by removing the qualifier from the function:

//  /--------------+--- referencia constante
//  v              v           funcion constante ---> vvvvv
const map<char,int>&  estado:: get_transiciones(void) const{
    return trans_;
//         ^^^^^^ <--- es constante porque get_transiciones es constante

Or without constant:

//           v <--- referencia
map<char,int>&  estado:: get_transiciones(void){
    return trans_;
//         ^^^^^^ <--- NO ES CONSTANTE
answered by 29.10.2017 / 23:17