C++ error C2662: “LinkNode<T> *List<T>::Locate(int)”: 不能将“this”指针从“const List<int>”转换为“List<int> &”
来源:互联网 发布:php foreach 编辑:程序博客网 时间:2024/05/18 06:09
错误 1 error C2662: “LinkNode<T> *List<T>::Locate(int)”: 不能将“this”指针从“const List<int>”转换为“List<int> &” e:\datastructure\cap_1\cap_1\singlelist.cpp 123 1 Cap_1
今天在写数据结构的单链表时遇到此错误,在网上查了相关的错误资料,并修改过来,次分享!
错误根本原因是const修饰的问题,下面结合代码分析具体原因:
template<class T>
LinkNode<T> * List<T>::Locate(int i) {
//定位函数 返回表中第i个节点的地址 如果i < 0 或者i 超过链表长度则返回NULL
if (i < 0) {
return NULL;
}
LinkNode<T> * current = first;
int m = 0;
while (current != NULL && m < i) {
current = current->link;
m++;
}
return current;
}
LinkNode<T> * List<T>::Locate(int i) {
//定位函数 返回表中第i个节点的地址 如果i < 0 或者i 超过链表长度则返回NULL
if (i < 0) {
return NULL;
}
LinkNode<T> * current = first;
int m = 0;
while (current != NULL && m < i) {
current = current->link;
m++;
}
return current;
}
template<class T>
bool List<T>::getData(int i, T & x) const{
//取出链表中第i个节点的data
if (i <= 0) {
return NULL; //数据非法返回false
}
LinkNode<T> * current = Locate(i); //借助定位函数直接定位到相应的节点
if (NULL == current) {
return false; //i超过单链表的长度返回false
}
else {
x = current->data;
return true;
}
}
bool List<T>::getData(int i, T & x) const{
//取出链表中第i个节点的data
if (i <= 0) {
return NULL; //数据非法返回false
}
LinkNode<T> * current = Locate(i); //借助定位函数直接定位到相应的节点
if (NULL == current) {
return false; //i超过单链表的长度返回false
}
else {
x = current->data;
return true;
}
}
这是这个单链表类中的定位函数和数据提取函数,开始的时候,我把数据提取函数定义为const修饰(然而定位函数没有),这个时候在用提取数据函数调用定位函数的时候报出上面的error,下面是网上的原因:
对象引用前加const报错:不能将“this”指针从“const a”转换为“a &”
const a &aa相当于一个const对象,由于const对象在调用成员函数的时候,会将this指针强行转换为const this,所以它将无法找到相应的const show()函数,并且编译器也无法将一个const的对象转化为一个普通对象来调用这个普通的show()方法,所以就会产生如题的编译错误。
结合自己的代码分析了一下这个原因,说白了就是,一个函数定义成了const修饰(即不能通过此成员函数修改成员变量),而如果此函数要嵌套调用其它成员函数,那么这个函数也必须是const修饰的,否则就会报error,其实也好理解,你用一不能修改成员变量的函数来调用可以修改成员变量的函数这本身就是矛盾的,所以C++中不允许这样做。
而网上的这个分析,属于分析道了本质,是一个const对象调用了非const函数,道理上是一样的,希望大家可以从中收获!
下面是改正后的代码(把被调用的函数用const修饰,使之统一)
相关链接 :http://blog.csdn.net/bagboy_taobao_com/article/details/5709414
阅读全文
1 0
- C++ error C2662: “LinkNode<T> *List<T>::Locate(int)”: 不能将“this”指针从“const List<int>”转换为“List<int> &”
- error C2662: “int Fraction::numerator(void)”: 不能将“this”指针从“const Fraction”转换为“Fraction &”
- error C2662: 不能将“this”指针从“const Node_align”转换为“Node_align &”解决方案
- error C2662: “CMaDirOperation::chang_file_type”: 不能将“this”指针从“const CMaDirOperation”转换为“CMaDirOperat
- C类型转换:error C2664:不能将参数 从“int”转换为“const char *”
- 不能将参数 1 从“int &(__thiscall RBTree<T>::* )(RBTreeNode<T> *)”转换为“int &(__cdecl *)(RBTreeNode<T> *)”
- error C2664: “int CListCtrl::InsertColumn(int,LPCTSTR,int,int,int)”: 不能将参数 2 从“const char [9]”转换为“LP
- error C2664: “int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [15]”转换为“LPTSTR”
- 用Python将list中的string转换为int
- int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [10]”转换为“LPTSTR”
- int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [10]”转换为“LPTSTR”
- int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [10]”转换为“LPTSTR”
- int & const t和 int const& t
- error C2664: “BOOL CDC::TextOutW(int,int,const CString &)”: 不能将参数 3 从“const char [18]”转换为“const CString &”
- error C2664: “BOOL CDC::TextOutW(int,int,const CString &)”: 不能将参数 3 从“const char [18]”转换为“const CStr
- error C2664: “BOOL CDC::TextOutW(int,int,const CString &)”: 不能将参数 3 从“const char [18]”转换为“const CStr
- error C2662: 无法将this指针从const转化为非const
- List<T>集合排序与Dictionary<int,string>排序
- Java练习题7
- Xposed hook原理
- 【机房合作】总结
- LeetCode日常刷题(2)
- 【死磕Java并发】-----J.U.C之阻塞队列:LinkedTransferQueue
- C++ error C2662: “LinkNode<T> *List<T>::Locate(int)”: 不能将“this”指针从“const List<int>”转换为“List<int> &”
- 巧妙设计一个mobile or pc 的登录界面
- 怎么将cocos2d-js 3.x 成功在android平台编译打包 (测试ok)
- LeetCode43_Multiply Strings
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛M题
- Linux系统该怎么学?运维老鸟的经验心得。
- 链表的带参数排序,可以通过姓名、成绩进行升序或降序操作
- 未排序正整数数组中累加和为给定值的最长子数组长度
- String 中的==