0

I have a class List which is a fixed size, linked List, and inside that class there is another class ListIterator which points to a specific element of that List. My problem is the c++ syntax I am not familiar with and the build errors from gcc are not helping me.

template<class T, int length>
class List {
public:
    //some stuff here
    //Row-struct

    template<class T>
    class ListIterator {
        public:
            ListIterator(int mPos, Row& mRow){pos = mPos; row = mRow;};
            T& operator *();
        private:
            int pos;
            Row& row;
    };

    typedef ListIterator<T> iterator;

    iterator begin();
private:
    int startIdx;
    Row storage[length];
};

template<class T, int length>
ListIterator List<T, length>::begin() {
    ListIterator *itor = new ListIterator(startIdx, storage[startIdx]);
    return itor;
}

I get the following errors:

error: shadows template parm 'class T'

  1. How can I make this typedef ListIterator<T> work?
  2. How can I use the same T I used for List for the Iterator?
  3. Why does my begin() not work?
3
  • 1
    you don't have to do anything, just use it, it's known in this scope. Commented Nov 9, 2017 at 11:33
  • But I want to use ìterator in ListIterator as well as in List as typedef Commented Nov 9, 2017 at 11:34
  • class ListIterator inside template <typename T> class List has access to T, it doesn't need to be a template. You could even directly name it class iterator and skip the typedef Commented Nov 9, 2017 at 13:00

1 Answer 1

1

Rename ListIterator template parameter T to something else, for example to TInner. Or make it a regular class if it can use T from outer class. Or move ListIterator outside of List class which would be a better idea since ListIterator does not actually do anything list-specific.

To fix begin function you need to use proper types:

template<typename T, int length> typename List<T, length>::iterator
List<T, length>::begin(void)
{
    iterator item_iterator(startIdx, storage[startIdx]);
    return(item_iterator);
}
Sign up to request clarification or add additional context in comments.

7 Comments

ListIterator has to know a few things about the List (pointer, next element, previous elemen etc.)
@binaryBigInt Your code does not demonstrate it though.
Another options is a trailing return type
@VTT How do I access the object in my main.cpp? List<int,2> list; List::iterator *it = list.begin(); doesn't work.
@binaryBigInt begin is supposed to return iterator, not a pointer to iterator. And you need to use proper type names (again), it should be List<int, 2>::iterator it = list.begin();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.