Object具有阻止匹配的类型限定符(未找到函数重载)

Tom*_*ica 6 c++ oop operator-overloading

我有一个简单的类,旨在将整数转换为字节数组.

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
};
Run Code Online (Sandbox Code Playgroud)

为了转换和更简单的使用,我决定添加operator=方法.但我认为我的实施mc_int& operator=(const mc_int&);是不正确的.

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)
}
Run Code Online (Sandbox Code Playgroud)

这可能是什么?我试过用other->value(),但那也错了.

Moo*_*uck 10

您的成员函数value()不是const函数,这意味着它有权修改成员,并且不能在const对象上调用.因为我认为你的意思是只读它,所以改成它int value() const;.然后你可以在const实例上调用它mc_int,它保证你不会意外地改变任何成员.

当它说"对象有类型限定符等等"时,这意味着对象有太多constvolatile限定符来访问函数.

此外,由于您发布了错误摘要,我假设您正在使用visual studio.Visual Studio在"错误"窗口中显示错误摘要.转到View-> Output以查看其可怕细节中的完整错误,这应该告诉您哪个变量是问题,并且由于它的原因而无法调用该函数const.