我正在编写一个类,嵌入一个可逆链表,并且我遇到了空指针类型的问题:
链接列表定义:
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 &'
我认为nullpointer是一个通用指针,可以用于任何目的吗?我做错了什么(为什么构造函数中没有编译错误?)
提前致谢
答案 0 :(得分:1)
get_successor()
返回LL
类型的值。它无法返回指针。您可能想要更改它:
LL* get_successor(){
if (index+1 <= size) {
return lList->succ;
} else { return nullptr; }
}