不能将“this”指针从“const Class(某个类)”转换为“Class &" 错误
来源:互联网 发布:excel2013快速选择数据 编辑:程序博客网 时间:2024/05/23 01:17
下面代码
class Student {public: const char * GetName(); Student(const Student & o);private: char name[20];};Student::Student(const Student & o){ const char *p = o.GetName(); int i = 0; for(;p[i]!='\0'; i++) name[i]=p[i]; name[i]='\0';}const char* Student::GetName(){ return name;}
使用VS2013编译程序有错误信息
error C2662: “const char *Student::GetName(void)”: 不能将“this”指针从“const Student”转换为“Student &”
const引用对象,会把this指针转化为为const this类型。
解决方法一: 将GetName改为const类型函数
class Student {public: const char * GetName() const; Student(const Student & o);private: char name[20];};Student::Student(const Student & o){ const char *p = o.GetName(); int i = 0; for(;p[i]!='\0'; i++) name[i]=p[i]; name[i]='\0';}const char* Student::GetName() const{ return name;}
解决方法二:
直接在内部将const修饰符去掉
Student::Student(const Student & o){ const char *p = (const_cast<Student*>(this))->GetName(); int i = 0; for(;p[i]!='\0'; i++) name[i]=p[i]; name[i]='\0';}
0 0
- 不能将“this”指针从“const Class(某个类)”转换为“Class &" 错误
- 不能将this指针从“const type" 转换为"type &"
- 不能将“this”指针从“const Vehicle”转换为“Vehicle &”
- 不能将“this”指针从“const CPaopao”转换为“CPaopao &
- 不能将“this”指针从“const Vector3D”转换为“Vector3D &" 错误
- error C2662: 不能将“this”指针从“const Node_align”转换为“Node_align &”解决方案
- error C2662: “CMaDirOperation::chang_file_type”: 不能将“this”指针从“const CMaDirOperation”转换为“CMaDirOperat
- error C2662: “int Fraction::numerator(void)”: 不能将“this”指针从“const Fraction”转换为“Fraction &”
- 【C++】不能将“this”指针从“const vector”转换为“vector & rhs"
- 对象引用前加const报错:不能将“this”指针从“const a”转换为“a
- 对象引用前加const报错:不能将“this”指针从“const a”转换为“a
- 对象引用前加const报错:不能将“this”指针从“const a”转换为“a
- 对象引用前加const报错:不能将“this”指针从“const a”转换为“a
- c++;对象引用前加const报错:不能将“this”指针从“const a”转换为“a &”
- [error] MFC错误不能将参数1从"const char [3]"转换为"const wchar_t *"
- 不能将“this”指针从“const XXX”转为“XXX &”
- C++ error C2662: “LinkNode<T> *List<T>::Locate(int)”: 不能将“this”指针从“const List<int>”转换为“List<int> &”
- vc2008错误:不能将参数 1 从“const char [5]”转换为“LPCTSTR”
- hdu5494
- Mahout-kmeans命令行文本文件聚类
- nginx空主机头
- 信管 数据库 网管 总结
- 机器学习(十二)学习SVM支持向量机过程中的引申
- 不能将“this”指针从“const Class(某个类)”转换为“Class &" 错误
- ROS中的depth image转换到laser scan的包
- 带上下界的网络流
- hdu 5505 GT and numbers【BestCoder Round #60】
- 编程之法之-字符串的旋转
- 【算法,算法】排序
- Android中使用Parcelable接口
- 关于录屏软件的实现原理的猜想
- 顺序表的创建及操作