• 0

[c++] Overloading operator[]


Question

For this assignment I am to overload the operator[] for a mini List class (basically a watered down list class). I can get the random access to the list by doing list[x] and getting the contents of node x. However when doing reassignments such as:

list[x] = 38

(the list is a list of integers)

I get this error on UNIX:

46: non-lvalue in assignment

I'm guessing that means there is no left hand value in the assignment. Here's my overloaded protocol & body.

(its a templated function)

Protocol:

T operator[](int m);

Body:

template <typename T>
T miniList<T>::operator[] (int m)
{
 ? ? ? ?// Allows random type access to the list much like an array
 ? ? ? ?dnode<T> *curr = (*this).header;
 ? ? ? ?T temp;

 ? ? ? ?for (int i = 0; i <= m; i++)
 ? ? ? ? curr = curr->next;

 ? ? ? ?temp = curr->nodeValue;

 ? ? ? ?return temp;
}

So I'm returning the type of data thats held inside the node that makes up the list. So I'm not sure if that statement list[x] = 38 is having a problem because of my overloaded operator[] or if it has something to do with the operator=. Any ideas?

Thanks in advance

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
For this assignment I am to overload the operator[] for a mini List class (basically a watered down list class). I can get the random access to the list by doing list[x] and getting the contents of node x. However when doing reassignments such as:

list[x] = 38

(the list is a list of integers)

I get this error on UNIX:

46: non-lvalue in assignment

I'm guessing that means there is no left hand value in the assignment. Here's my overloaded protocol & body.

(its a templated function)

Protocol:

T operator[](int m);

Body:

template <typename T>
T miniList<T>::operator[] (int m)
{
 ? ? ? ?// Allows random type access to the list much like an array
 ? ? ? ?dnode<T> *curr = (*this).header;
 ? ? ? ?T temp;

 ? ? ? ?for (int i = 0; i <= m; i++)
 ? ? ? ? curr = curr->next;

 ? ? ? ?temp = curr->nodeValue;

 ? ? ? ?return temp;
}

So I'm returning the type of data thats held inside the node that makes up the list. So I'm not sure if that statement list[x] = 38 is having a problem because of my overloaded operator[] or if it has something to do with the operator=. Any ideas?

Thanks in advance

584785696[/snapback]

I think you need to return a reference to the element, not the element itself. I've never overloaded [] before, :) sue me if I'm wrong. :)

...
T& miniList<T>::operator[] (int m)
...

You may also want to track how many elements are in your list so you don't go off the end.

Wouldn't operator= overload be assigning a miniList to a miniList? So I doubt it's your problem with subscript assignment.

Edited by weenur
Link to comment
Share on other sites

  • 0

Yeah, I believe you do have to return a reference to the value. Also, you may want to add some checks, like if the argument, m, is less than zero or greater than the number of nodes in the list.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.