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.
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 const
ness.
Try changing:
int value(); //Returns value
To:
int value() const; //Returns value
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With