对象具有防止匹配的类型限定符(未找到函数重载)
Object has type qualifiers that prevent match (function overload not found)
我有一个简单的类,用于将整数转换为字节数组。
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
};
为了转换和更简单的使用,我决定添加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)
}
这可能是什么?我试图使用other->value()
,但这也是错误的。
你的成员函数value()
不是const
函数,这意味着它有修改成员的权利,不能在const
对象上调用。由于我假设您的意思是只读,将其更改为int value() const;
。然后你可以在mc_int
的const
实例上调用它,它保证你不会意外地改变任何成员。
当它说"对象有类型限定符什么的"时,这意味着对象有太多的const
或volatile
限定符来访问函数。
另外,既然你发布了一个错误的摘要,我假设你正在使用visual studio。Visual Studio在"Error"窗口中显示错误摘要。转到View->Output查看完整的错误细节,它应该告诉您哪个变量是问题所在,并且由于它的const
性而无法调用该函数。
尝试更改:
int value(); //Returns value
:
int value() const; //Returns value
在这个重载的operator=
中,other
是const
的引用。你只能调用这个对象上标记为const
的成员函数。由于value()
函数只返回val
而不修改对象,因此应该将其标记为const
:
int value() const;
这表示该函数不会修改对象的状态,因此可以在const
对象上调用。
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 可以打印矢量和矢量中的矢量的非重载C++函数
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 使用模板重载函数
- C++线程中,没有重载函数接受 X 参数
- std::vector 没有重载函数的实例与参数列表匹配
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 对重载函数find_first_not_of的不明确调用
- 如何从重载解析中删除重载函数?
- CUDA:重载函数"isnan"的多个实例
- C++派生类重载函数(带有 std::function 参数)不可见
- 避免在人为的重载函数调用中拼写出类型
- C++:如何为多个重载函数保留通用代码路径?
- 什么时候可以使用常量装饰调用我的重载函数?
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- std::调用,未找到匹配的重载函数
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 为什么使用不匹配的参数调用重载函数仍然有效
- 如何通过签名作为模板参数来解决重载函数?