Templates and Operator Overloading

I'm having trouble overloading the [] operator in a Linked List class I'm making. The class is templated, so I'm not sure exactly how to overload it. Every time I make a correction, I get a new error. What am I doing wrong?

List.h:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef LIST_H
#define LIST_H

#include <assert.h>

namespace LList {
    // Begin Node Structure
    template< typename T >
    struct node {
        T data;
        node<T> * next;
    };
    // End Node Structure

    // Begin List Class Declaration
    template< typename T >
    class List {
        public:
            List();
            ~List();

            // Retrieval Functions
            int getSize();
            T get( int pos );
            T operator[]( int pos );

            // Modification Functions
            void add( T data );
            void destroy();
        private:
            node<T> * first;
            node<T> * last;

            int size;
    };
    // End List Class Declaration

    // Begin List Class Definitions
    template< typename T >
    List<T>::List() {
        this->first = NULL;
        this->last = NULL;
    }

    template< typename T >
    List<T>::~List() {
        destroy();
    }

    template< typename T >
    int List<T>::getSize() {
        return this->size;
    }

    template< typename T >
    T List<T>::get( int pos ) {
        node<T> * temp = first;
        int currentPos = 0;

        assert( !( pos > ( this->size - 1 ) ) || ( pos < 0 ) );
        while( temp != NULL ) {
            if( currentPos == pos ) {
                return temp->data;
            } else {
                currentPos++;
                temp = temp->next;
            }
        }

        return NULL;
    }

    template< typename T >
    T List<T>::operator[]( int pos ) {
        return get( pos );
    }

    template< typename T >
    void List<T>::add( T data ) {
        node<T> * temp = new node<T>;
        temp->data = data;
        temp->next = NULL;

        if( this->first == NULL ) {
            this->first = temp;
            this->last = temp;
            size = 1;
        } else {
            this->last->next = temp;
            this->last = temp;
            size++;
        }
    }

    template< typename T >
    void List<T>::destroy() {
        node<T> * temp = new node<T>;

        while( this->first != NULL ) {
            temp = this->first;
            this->first = this->first->next;
            delete temp;
        }

        this->size = 0;
    }
    // End List Class Definitions
}

#endif // LIST_H 


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using std::cout;
using std::endl;
#include "includes/List.h"
using namespace LList;

int main( int argc , char ** argv ) {
    List<int> * myList = new List<int>();

    myList->add( 5 );
    myList->add( 2 );
    myList->add( 8 );
    myList->add( 3 );
    myList->add( 1 );

    int pos = 2;
    cout << "Get(" << pos << "): " << myList->get( pos ) << endl;
    cout << "Position [" << pos << "]: " << myList[pos] << endl;

    return 0;
}


And here's my error list:

/home/packetpirate/Documents/C++ Projects/List/main.cpp||In function ‘int main(int, char**)’:|
/home/packetpirate/Documents/C++ Projects/List/main.cpp|18|error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Position [")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](pos))), ((const char*)"]: ")) << *(myList + ((unsigned int)(((unsigned int)pos) * 12u)))’|
/usr/include/c++/4.4/ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|117|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|127|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|165|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|169|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|173|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/bits/ostream.tcc|91|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|180|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/bits/ostream.tcc|105|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|191|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|200|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|204|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|209|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|213|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|221|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/ostream|225|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.4/bits/ostream.tcc|119|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]|
||=== Build finished: 1 errors, 0 warnings ===|
This isn't quite correct
cout << "Get(" << pos << "): " << myList->get( pos ) << endl;
cout << "Position [" << pos << "]: " << myList[pos] << endl;


my list is a pointer
List<int> * myList = new List<int>();
so
myList[pos]
is a standrad ponter dereference - which
will return a
List<int>
object - which will cause an error as you don't
have the << operator overloaded for a List object.

It should be
cout << "Get(" << pos << "): " << myList->get( pos ) << endl;
cout << "Position [" << pos << "]: " << (*myList)[pos] << endl;



PS
Once you have corrected that - you may find the compiler
gives a Warning for the
T List<T>::get( int pos )
function - because
of the
return NULL
statement which is converting a pointer to an integer.
Topic archived. No new replies allowed.