我正在尝试创建一个双向链接列表,并使用一个接受通过引用传递的值的函数。但是,当我尝试访问该值时会引发错误。我收到“错误:左值必须作为赋值的左操作数&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;
}
答案 0 :(得分:0)
行
&da= NULL;
正在尝试将NULL设置为变量da
的地址。你不能那样做。
您可能是说
this->data = &da;
这将work
(如在编译中一样),但是如果以data
传递的字符串超出列表的作用域,则可能会导致错误(很可能)。
如果您要使用string*
,则可能真正想要的是
this->data = new string(da);
,它动态分配一个新字符串,并为其提供da
来进行复制。然后,在析构函数中,您需要
if (data != nullptr) delete data;
我不是Standards专家,所以不能给您lvalues
这样的技术解释。