Alex Rivera | Logout

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

Asked 2013-03-16T00:39:52.173
10

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.

Edit
Report

1 Answer

18

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.

answered 2013-03-16T00:42:15.667

Your Answer