0
template <class TYPE>
class DList
{
    //Declaring private members
    private:
    unsigned int m_nodeCount;
    Node<TYPE>* m_head;
    Node<TYPE>* m_tail;

    public:
    DList();
    DList(DList<TYPE>&);
    ~DList();
    unsigned int getSize();
    void print();
    bool isEmpty() const;
    void insert(TYPE data);
    void remove(TYPE data);
    void clear();
    Node<TYPE>*  getHead();
    ...
    TYPE operator[](int); //i need this operator to both act as mutator and accessor
};

i need to write a template function which will do the following process:

// Test [] operator - reading and modifying data
cout << "L2[1] = " << list2[1] << endl;
list2[1] = 12;
cout << "L2[1] = " << list2[1] << endl;
cout << "L2: " << list2 << endl;

my code cant work with

list2[1] = 12;

i get error C2106: '=' : left operand must be l-value ERROR. i want the [] operator to be able to make list2's first index node value 12

MY CODE:

template<class TYPE>

     TYPE DList<TYPE>::operator [](int index) 
    {
        int count = 0;
        Node<TYPE>*headcopy = this->getHead();
        while(headcopy!=nullptr && count!=index)
        {
            headcopy=headcopy->getNext();
        }

        return headcopy->getData();
    }
Zuh Menzo
  • 1
  • 2
  • Operator `[ ]` usually has two overloads, not one. You need to implement both. [See example here](http://en.cppreference.com/w/cpp/container/vector/operator_at) – PaulMcKenzie Dec 02 '17 at 22:50
  • Also, your code won't work if `DList` is passed as `const` and you tried to use `[ ]` in any aspect. That's why you need the second overload. – PaulMcKenzie Dec 02 '17 at 22:56
  • can you show me in the example? – Zuh Menzo Dec 02 '17 at 22:57
  • `void SomeFunc(const DList& d) { std::cout << d[0]; }` -- Try that. None of the functions for `[ ]` will work, even the one you claim is working now. Instead you will get a compiler error. To solve that, you need the `const` overloaded version. – PaulMcKenzie Dec 02 '17 at 22:59

1 Answers1

1

my code cant work with

list2[1] = 12;

i get error C2106: '=' : left operand must be l-value ERROR. i want the [] operator to be able to make list2's first index node value 12

In C++, we have what is known as Value Categories. You should make the operator return by reference. Hence, change your declaration from:

TYPE operator[](int);

to:

TYPE& operator[](int);

I am assuming that headcopy->getData(); equally returns a reference to a non-local variable.


As PaulMcKenzie noted, you'll equally need an overload that works with a const this, aka, const member function overload. Hence we have:

TYPE& operator[](int);
const TYPE& operator[](int) const;

See What is meant with "const" at end of function declaration? and Meaning of "const" last in a C++ method declaration?

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • `[ ]` will still fail to work if `DList` is passed as `const` to a function. – PaulMcKenzie Dec 02 '17 at 22:56
  • so besides TYPE& operator[](int); this implementation, i need a second implementation for my [ ] operator to work for list2[1] = 12;? – Zuh Menzo Dec 02 '17 at 23:06
  • @ZuhMenzo, the `const` *member-function* overload is needed to make that operator work when calling it from `const` object. See the links in my answer – WhiZTiM Dec 02 '17 at 23:12
  • i got your point but still list2[1] = 12; when this line is executed regardless list 2 const or not 1st nodes value must become 12 when i debug it still enters TYPE& operator[](int); this function – Zuh Menzo Dec 02 '17 at 23:14
  • i didnt overload = operator so i somehow need to understand when [ ] is used with = or not – Zuh Menzo Dec 02 '17 at 23:21
  • i understood now damn this is clever as hell, thanks – Zuh Menzo Dec 02 '17 at 23:48