Error: the object has type qualifiers that are not compatible with the function

1

I have a Rational class:

class Racional
{

private: 
    int m_numerador;
    int m_denominador;

public:
    Racional();
    Racional(int numerador, int denominador);
    void setNumerador(int numerador);
    void setDenominador(int denominador);
    int getNumerador();
    int getDenominador();
    bool valido();
}

Definition of valid method ():

bool Racional::valido() {
if (m_denominador == 0) {
    return false;
}
else return true;
}

Now using this class I want to create a function to tell me if the rational number is valid:

bool operaciones(const Racional &r1, const Racional &r2){
//ahora cuando hago esto me salta un error:
r1.valido();
}

Could you explain why?

Thank you.

    
asked by Chariot 19.09.2018 в 00:34
source

1 answer

2
  

I want to create a function to tell me if the rational number is valid:

bool operaciones(const Racional &r1, const Racional &r2){
    //ahora cuando hago esto me salta un error:
    r1.valido();
}
     

Could you explain why?

The r1 object is read-only:

bool operaciones(const Racional &r1, const Racional &r2)
//               ~~~~~ <-- Está marcado como constante (sólo lectura)

But the Racional::valido function is not read-only:

class Racional
{
    // ...

    // ...
    bool valido(); // <-- función "normal".
}

To solve this, mark the function as constant (read only):

class Racional
{
    // ...

    // ...
    bool valido() const; // <-- función sólo lectura.
}

Remember to mark it also const in the definition.

    
answered by 19.09.2018 / 08:40
source