Please i'm working on a linked list and want to overload the ++
operator in order to use a dummy pointer to loop/traverse through the list.
But its gives me a compiled-time error.
Here is the actual code:

class Node
{
    private:
    string value;
    Node* next;

    public:
    ~Node(){del ete this;};
    Node* operator++(int);
    Node* operator++();
  }

  Node* Node::operator++()
    {
      this = this->next; //error message
      return this;
    }

  Node* Node::operator++(int)
    {
      Node* nodeptr = this;
      this = this->next;  //error message
      return (nodeptr);
    }

error message = "lvalue required as left operand of assignment".

Recommended Answers

All 3 Replies

The concept of incrementing a linked list is somewhat misguided. Most likely you intend to increment an iterator over the list. The standard library makes heavy use of this concept.
To support that you would need to program the interator into your linked list interface. In general, you provide the following

begin()
end()
==
++
->
*

You can learn more if you google c++ iterators. Here is a starting point.

Are you kidding me? You have written ~Node(){delete this;};. This is wrong on so many levels. You should not do this, you don't need to do this, and it will most probably crash your program when you run this.

You have to consider the "this" pointer as a constant, in fact, it is a constant. Its actual type is either Node * const or const Node * const (if the member function is declared const).

You cannot modify the this pointer inside a member function. This is what the compiler is telling you when it says "lvalue is required as left operand of assignment". An lvalue means a variable that can be modified, and this does not qualify as an lvalue.

As L7Sqr suggested, you need to create an iterator class that stores a pointer to a Node and then you can implement the increment operator (and dereferencing operators) as you already did.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.