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();
}