0

I'm trying to implement overloading of the subscript operator in my doubly linked list class, but I am facing problems I've been unable to overcome by myself. I am also pretty new to C++.

This is what I have now.

Outtake from DList class:

T &operator[](int index) {
    lookAt = root;
    for (size_t i = 0; i < index; i++) {
        lookAt = lookAt->getNext();
    }
    return lookAt->getItem();
}

Node<T>* root;
Node<T>* lookAt;
Node<T>* temp;

Node class:

template <class T>
class Node {
public:

Node() {
    this->setNext(nullptr);
    this->setPrev(nullptr);
}

Node *getNext() const {
    return next;
}

void setNext(Node *next) {
    Node::next = next;
}

Node *getPrev() const {
    return prev;
}

void setPrev(Node *prev) {
    Node::prev = prev;
}

T getItem() const {
    return item;
}

void setItem(T item) {
    Node::item = item;
}

private:

Node* next;
Node* prev;
T item;
};

The error I keep getting is this: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' return lookAt[index].getItem();

Which leads me to believe there's some kind of problem with the way my item variable is referenced, or/and the return part of the overloading function.

Would appreciate any help/guidance with this.

Cheers

3
  • Either getItem() needs to be changed to return T&, or operator[] needs to return T, or the latter needs to be implemented without using the former. Actually, you probably want two overloads of getItem() - a non-const one returning T&, and a const one returning either T or const T&. Commented Sep 13, 2015 at 13:46
  • Strange that lookAt and temp are member variables instead of local variables. Commented Sep 13, 2015 at 13:46
  • Thanks for commenting! Changing getItem() to return T& and removing const solved the problem. Am I right in thinking that the const have to be removed because the reference to item must be able to change? And yes, those variables should probably be local. Commented Sep 13, 2015 at 14:41

1 Answer 1

0

Your getItem() returns a T-type variable, which is a copy of the real data, item. (rvalue)

Your [] operator tries to return an T&, which is an lvalue (reference). However, we can't transform rvalue into lvalue.

One possible solution is to return T& in getItem(), like this:

T& getItem() {
    return item;  // Now returns a reference (lvalue).
}

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! It did. Although I had tried this before - turns out I had to remove the const part from the getItem-function as well. The function was an auto-generated getter though, so removing things might not be the best solution I suspect.. Any idea what caused this?
Const functions prohibit possible change to variables, and the [] operator isn't constant so that it may cause changes. Just like the comment above, you might want two functions that return different things. The auto-generated function may suppose that you want to modify your data through the set function, so the get function only returns a rvalue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.