0

I am attempting to create a doubly linked list and use a function which accepts a value passed by reference. However, when I try to access the value it throws an error. I am getting the "error: lvalue required as left operand of assignment &da= NULL;"

I have tried:

 #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;
 }
  • Unrelated: Believe it or not, `__DOUBLYLINKEDLIST_H__` is an illegal identifier. Any identifier with two underscores in a row anywhere in it is reserved. More details: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Feb 08 '19 at 02:21
  • should I change it to one underscore? that part of the code was given by my instructor – Raquel de Anda Feb 08 '19 at 02:57
  • Preceding underscore at global scope is illegal, so `_DOUBLYLINKEDLIST_H`. – user4581301 Feb 08 '19 at 03:00

1 Answers1

0

The line

&da= NULL;

is trying to set NULL to the address of the variable da. You can't do that.

You might mean

this->data = &da;

which would work (as in, compile), but would probably cause errors if the string passed as data goes out of scope before your list does (which is very likely).

What you probably actually want, if you're going to use a string*, is

this->data = new string(da);

which dynamically allocates a new string, giving it da to copy from. In the destructor, you'd then want something like

if (data != nullptr) delete data;

I'm not a Standards guy, so can't give you the technical explanation of lvalues and such.

Phil M
  • 1,619
  • 1
  • 8
  • 10