c++:赋值中的非ivalue
c++: non-Ivalue in assignment
嗨,我想做一个外部单链表。我有一个问题,"非我值在分配",它发生在网上"this = currP->next"我试着使它currP。接下来,它也会产生一个错误
#include <cstdlib>
using namespace std;
struct node{
int data;
node *next;
node(int i){
data = i;
next = NULL;
}
void insert(int position, node &n){
node *currP = this;
node *prevP= NULL;
for(int counter = 0; counter>=position;counter++, prevP = currP, currP = currP->next){
if(counter==position)
{
n.next = currP->next;
currP->next = &n;
}
}
}
void add(node &n){
next = &n;
}
void deleteNode(int i){
node *currP = this;
node *prevP = NULL;
while(currP!= NULL){
if(currP->data == i){
if(prevP == NULL)
this = currP->next;
else{
prevP->next = currP->next;
}
}
prevP = currP;
currP = currP->next;
}
}
};
lvalue
是一个可以驻留在等号运算符左侧的变量。这意味着它的值可以改变。您不能更改this
的值,这是不允许的,因此出现错误。
你可以这样重写你的函数:
node* deleteNode(int i){
if ( this->data == i )
return this->next;
else
{
if ( this->next )
this->next = this->next->deleteNode(i);
else
return this;
}
}
deleteNode()
现在将返回一个指向列表其余部分开始的指针,递归算法将第一部分与最后一部分连接起来。它没有经过测试,所以一些调整可能是必要的,但我希望你能明白。
左值是语义规则。它的意思是"左值"
左值的例子有:
- 一个变量。即"a"
- 内存地址。"a[4]"或"*(a+8)"
this
不是左值。你不能给它赋值。它是对方法调用者的引用。
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 用C++中的sscanf赋值
- 为std::string的某个索引赋值
- 重载Singly Linked List中的赋值运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- gtest_使用setargpointee在函数中赋值
- 非常量变量只读位置的赋值
- 使用赋值运算符重载从类中返回jobject
- C++数据文件、数组和计算赋值
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 标准库类型的赋值运算符的引用限定符
- 关于 c++ 函数中指针赋值的简单问题
- 复制构造函数、赋值运算符C++
- 标准::变体的赋值运算符
- cin >> int 给定一个字符串将 int 赋值为 0
- if 子句中的赋值不起作用
- c++:赋值中的非ivalue