一般原因是常量对象尝试调用该对象里的非常量函数。常量对象只能访问常量函数,非常量对象可以访问常量函数和非常量函数。 class A { public: int age; std::string name; public: void getName() const; void getAge(); } int main() { const A a; a.getName(); a.
String(const String&rs){ len=rs.getlen();//***在这里报错 } 因为rs是个const对象,所以为了避免该对象被改变,要求getlen函数也必须是常成员函数。修改下getlen函数的定义即可:int getlen()const {return len;}//这样就行了 另外要包含个头文件#include<cstring> ...
我想到的一种办法是可以直接在内部将const修饰符去掉,具体如下: out<<(const_cast<Point3d*>(this))->GetX()<<""<<(const_cast<Point3d*>(this))->GetY()<<""<<(const_cast<Point3d*>(this))->GetZ()<<std::endl; 参考文献: 1.Error C2662, cannot convert‘this’ pointer from‘const class...
由于const对象在调用成员函数的时候,会将this指针强行转换为const this,所以它将无法找到相应的const get函数,并且编译器也无法将一个const的对象转化为一个普通对象来调用这个普通的get方法,所以就会产生如题的编译错误。 至于const的用法,我们已经已经在C++/const详解中详细的介绍了const的用法,这里就不再赘述。 解决...
Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’的解决办法。... 是金子就会灿烂 1 8472 相关推荐 SqList class 实现 2007-06-03 01:16 − SqList.h 1 #ifndef SQLIST_H 2 #define SQLIST_H 3 4 //#include 5 6 #defin... 中土 0 4948 C++之...
Hi! I've been developing a CBox class with all kinds of operations working with on Boxes and I'm getting a compiler error I don't understand. The error is "'CBox::volume' : cannot convert 'this' pointer from 'const CBox' to 'CBox &'...
aerror C2662: 'GetCfgName' : cannot convert 'this' pointer from 'const class CMICfg' to 'class CMICfg &' 错误C2662 :‘GetCfgName’ : 不能转换‘这’尖从‘const类CMICfg’成‘类CMICfg &’ [translate] 英语翻译 日语翻译 韩语翻译 德语翻译 法语翻译 俄语翻译 阿拉伯语翻译 西班牙语翻译 ...
在编程中,你可能会遇到一个错误信息:“cannot convert parameter 1 from 'const int' to 'long []'”。这个错误提示你定义的函数中第一个参数类型为long数组,但在调用该函数时传递的却是const int类型。在C++或类似的编程语言中,函数参数的类型需要严格匹配。long数组和const int之间存在类型不匹配...
你定义的函数中第一个参数类型为long [],但是你调用时为const int型。cannot
voidfun_2()const{ std::cout <<"非常量函数"<< std::endl; } }; intmain() { constA a; a.fun_1();//编译报错, a.fun_2();//编译通过 } 上面这个简单得代码,可以说明这个问题。 对于实例化对象a, 被定义为常量对象,因此可以调用调用fun_2(), 不能调用fun_1() ...