Problem overloading an operator.

Hi,

I'm working on a project for school, and I need to overload the [] operator to allow the nodes of a linked list to act as subscripts, as in an array. This is what I have implemented:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
template <class T>
ListNode<T>& LinkedList::operator[]( T &sub  ){
     ListNode<T> *nodePtr;
     int tNodes=0;

     nodePtr=head;  
     while( nodePtr ){
          tNodes++;
          nodePtr= nodePtr->next;
     }
         
     if(sub >(tNodes-1) || < 0){
          throw "Invalid subscript.";
          return;
     }
         
     if(sub == 0){
          return head;
     }
     
     nodePtr=head;
     
     for(int x=0; x<=sub; x++){
          nodePtr=nodePtr->next;
          return nodePtr;
     } 
}


The problem is that I get two errors when I try to compile:

error: ‘template<class T> class LinkedList’ used without template parameters

error: ‘ListNode<T>& operator[](T&)’ must be a nonstatic member function


They seem pretty straightforward, but I have can't figure them out.
Any suggestions would help.

Thanks.
Instead of

1
2
template <class T>
ListNode<T>& LinkedList::operator[]( T &sub  ){


you shall write

1
2
template <class T>
ListNode<T>& LinkedList<T>::operator[]( T &sub  ){


Also I think it would be simply to write

1
2
template <class T>
ListNode<T>& LinkedList<T>::operator[]( size_t index  ){


that is to use type size_t (or int) for the parameter of the operator.
Instead of

1
2
template <class T>
ListNode<T>& LinkedList::operator[]( T &sub  ){


you shall write

1
2
template <class T>
ListNode<T>& LinkedList<T>::operator[]( T &sub  ){


Also I think it would be simply to write

1
2
template <class T>
ListNode<T>& LinkedList<T>::operator[]( size_t index  ){


that is to use type size_t (or int) for the parameter of the operator.
Wow, thanks. I didn't realize i forgot the <T>. That's sad considering I remembered it on other functions I made. And yes, I'm just going to use an int since a template doesn't really make sense.
Topic archived. No new replies allowed.