Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object has type qualifiers that prevent match (function overload not found)

I have a simple class, intended to convert integers to byte arrays.

class mc_int {
    private: 
        int val;      //actual int
    public: 
        int value();  //Returns value
        int value(int);  //Changes and returns value
        mc_int();  //Default constructor
        mc_int(int);//Create from int
        void asBytes(char*); //generate byte array

        mc_int& operator=(int);
        mc_int& operator=(const mc_int&);

        bool endianity;  //true for little
};

For conversion and simpler usage, I decided to add operator= methods. But I think my implementation of mc_int& operator=(const mc_int&); is incorrect.

mc_int& mc_int::operator=(const mc_int& other) {
            val = other.value();  
            //    |-------->Error: No instance of overloaded function matches the argument list and object (object has type quelifiers that prevent the match)
}

What could this be? I tried to use other->value(), but that was wrong too.

like image 765
Tomáš Zato - Reinstate Monica Avatar asked Mar 16 '13 00:03

Tomáš Zato - Reinstate Monica


3 Answers

Your member function value() is not a const function, which means it has the right to modify the members, and cannot be called on const objects. Since I assume you meant it to be read only, change it to int value() const;. Then you can call it on const instances of mc_int, and it guarantees you don't accidentally change any members.

When it says "Object has type qualifiers blah blah", that means an object has too many const or volatile qualifiers to access a function.

Also, since you posted a summary of an error, I assume you're using visual studio. Visual Studio shows summaries of errors in the "Error" window. Go to View->Output to see the full errors in their terrible detail, which should have told you which variable was the problem, and that it couldn't call that function because of it's constness.

like image 65
Mooing Duck Avatar answered Nov 03 '22 08:11

Mooing Duck


Try changing:

int value();  //Returns value

To:

int value() const;  //Returns value
like image 40
Jorge Israel Peña Avatar answered Nov 03 '22 08:11

Jorge Israel Peña


In this overloaded operator=, other is a const reference. You can only call member functions that are marked const on this object. Since the value() function just returns val and doesn't modify the object, it should be marked as const:

int value() const;

This says that this function won't modify the state of the object and can therefore be called on const objects.

like image 3
Joseph Mansfield Avatar answered Nov 03 '22 07:11

Joseph Mansfield