我正在编写一个类,嵌入一个可逆链表,但我遇到了空指针类型的问题:
链表定义:
struct LL {
int information;
LL* pre;
LL* succ;
};
类定义(部分):
class T_LL {
private :
LL *lList;
int index;
int size;
public :
T_LL() {
lList = new(LL);
lList->pre = nullptr;
lList->succ = nullptr;
size = 0;
index = -1;
}
...
LL get_successor(){
if (index+1 <= size) {
return *lList->succ;
} else { return nullptr; }
}
当我尝试编译它时,编译器提示 get_successor()
方法,说:
error C2664: 'LL::LL(const LL &)' : cannot convert argument 1 from 'nullptr' to 'const LL &'
我以为空指针是一个通用的指针,可以用于任何目的?我做错了什么(为什么构造函数中没有编译错误)?
提前致谢
最佳答案
get_successor()
返回类型为 LL
的值。它不能返回一个指针。您可能想要更改它:
LL* get_successor(){
if (index+1 <= size) {
return lList->succ;
} else { return nullptr; }
}
关于c++ - 创建面向对象的链表时出现编译问题(编译器错误 C2664),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022569/