I'm trying to overload the [] operator for my linked list class so that you can access an index with it, rather than having to use the get() function in the class. I'm pretty sure I overloaded this right:
1 2 3 4 5 6 7 8 9 10 11 12
template< typename T >
T LinkedList<T>::operator[](int pos) {
node<T> *current = new node<T>;
int i = 0;
for(current = first;current != NULL;current = current->link) {
if(i == pos) {
return current->data;
}
i++;
}
}
However, I know I'll also have to overload other operators, such as <<, if I want to, for example, use cout to output the data returned by that index. How can I overload the << operator and = operator this way?
This is an error and causes a memory leak: node<T> *current = new node<T>;
However, I know I'll also have to overload other operators, such as <<, if I want to, for example, use cout to output the data returned by that index. How can I overload the << operator and = operator this way?
No, why do you think that? operator[] returns an object of type T, so as long as these operators exist for that type, everything's fine.
Funny, because Visual Studio seems to think otherwise.
cout << myList[3] << endl;
Error:
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'linklist::LinkedList<T>' (or there is no acceptable conversion) c:\users\packetpirate\documents\visual studio 10\projects\linkedlist_header\linkedlist_header\main.cpp 39 1 LinkedList_Header
2 IntelliSense: no operator "<<" matches these operands c:\users\packetpirate\documents\visual studio 10\projects\linkedlist_header\linkedlist_header\main.cpp 39 7 LinkedList_Header
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'linklist::LinkedList<T>' (or there is no acceptable conversion)
What is myList?
Either it's a list of a list, or your [] operator is returning a list instead of an individual element.
Also, how does:
node<T> *current = new node<T>;
...cause a memory leak? It works fine...
If you create something with new, you have to delete it. Otherwise whatever you created never goes away. So every time you call the [] operator, it's newing a node, which sucks up memory that never gets release. So with every call, your program uses more and more memory.
Hence, memory leak.
You don't even need to use new there. What's the point of creating a new node in that operator? All you do is point to existing nodes. Just change that line to this:
The hell? There's no way that's happening. The offset operator has a higher precedence than the shift operators. Either you're not showing us all the relevant code, or that errors points to a different line.
It works fine...
"Working" and "working right now" are two very different conditions. Your code works until the operator is used enough times. After spending an indeterminate amount of time allocating memory and never freeing it, something very bad will happen to the program.
You don't need to allocate an object at that point, evidenced by the fact that you almost immediately overwrite the pointer with a pointer to the head node. Just initialize current to zero.
Here's the problem: myList is a pointer, not an object. When you do myList[x], you're not calling the operator[] overload and passing it myList as this and x as the index; you're trying to access the LinkedList<int> object that's x elements past the start of the array myList points to. In other words, *(myList+x). This evaluates to a LinkedList<int>.
Just one more question... if I wanted to still make it a pointer to an object, how could I called the [] operator on the object without the parentheses and * sign? Like: myList[3]?
@Disch, I was referring to this piece of code I edited out:
LinkedList<int> myList();
I was wondering why it was giving me an error. The reason is that unless I'm calling an overloaded constructor, and I'm just calling the default constructor, the parentheses shouldn't be there.