错误提示:
错误 C2662 “int credit_card::calc_free_days(boost::gregorian::date)”: 不能将“this”指针从“const credit_card”转换为“credit_card &”chapter2 d:\project\boost\chapter2\eg_date_time\eg1.cpp24
该错误同时还会提示消息:对象含有与成员函数不兼容的类型限定符。
错误消息
“function”: 不能将“this”指针从“type1”转换为“type2”
编译器不能将 this 指针从 type1 转换为 type2。
此错误可能是由对 const 对象调用非 const 成员函数引起的。可能的解决方案:
-
从对象声明中移除 const。
-
将 const 添加到成员函数中。
下面的示例生成 C2662:
// C2662.cpp class C { public: void func1(); void func2() const{} } const c; int main() { c.func1(); // C2662 c.func2(); // OK }