如何解决“错误:左值要求作为分配的左操作数”

问题描述 投票:0回答:1

我试图创建一个双向链表,并使用它接受通过引用传递值的函数。然而,当我尝试访问值,它抛出一个错误。我收到“错误:左值要求作为分配及DA = NULL的左操作数;”

我努力了:

 #ifndef __DOUBLYLINKEDLIST_H__
 #define __DOUBLYLINKEDLIST_H__
 //
 //
 #include
 #include
 using namespace std;

 class DoublyLinkedList {
 public:
 DoublyLinkedList();
 ~DoublyLinkedList();
 void append (const string& s);
 void insertBefore (const string& s);
 void insertAfter (const string& s);
 void remove (const string& s);
 bool empty();
 void begin();
 void end();
 bool next();
 bool prev();
 bool find(const string& s);
 const std::string& getData() const;

 private:
 class Node
 {
 public:
 Node();
 Node(const string& data);
 ~Node();
 Node* next;
 Node* prev;
 string* data;
 };
 Node* head;
 Node* tail;
 Node* current;
 };

 DoublyLinkedList::Node::Node(const string& da)
 {
 this->data=nullptr;
 this->next=nullptr;
 this->prev=nullptr;
 &da= NULL;
 }
c++ doubly-linked-list
1个回答
0
投票

该生产线

&da= NULL;

试图NULL设置为可变da的地址。你不能做到这一点。

你可能意味着

this->data = &da;

这将work(如,编译),但如果作为data传递的字符串超出范围列表之前可能会导致错误做(这是很可能的)。

你可能真正想要什么,如果你要使用string*,是

this->data = new string(da);

其动态分配一个新的字符串,给它da从复制。在析构函数,你会再想要像

if (data != nullptr) delete data;

我不是一个标准的家伙,所以不能给你lvalues和这样的技术解释。

© www.soinside.com 2019 - 2024. All rights reserved.