我正在尝试创建一个双向链表,并使用一个接受通过引用传递的值的函数。但是,当我试图访问该值时,它抛出了一个错误。我得到"error: lvalue required as left operand of assignment &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;
}发布于 2019-02-08 10:05:30
这条线
&da= NULL;正在尝试将NULL设置为变量da的地址。你不能这么做。
你的意思可能是
this->data = &da;这将编译(就像在work中一样),但是如果在列表之前作为data传递的字符串超出了作用域(这很有可能),那么它可能会导致错误。
如果您要使用string*,您可能真正想要的是
this->data = new string(da);它动态地分配一个新的字符串,给它一个可供复制的da。在析构函数中,你需要类似这样的东西
if (data != nullptr) delete data;我不是一个标准专家,所以我不能给你lvalues之类的技术解释。
https://stackoverflow.com/questions/54585074
复制相似问题