错误 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{
bool List<T>::getData(int i, T & x) const{