views:

558

answers:

1

I'm a noob experimenting with templates and design patterns and would like to create an iterator pattern using a template argument ITEM. However, my private member var is a STL container that is itself a template, so how can I 'sync' the template argument of the STL container to that used by the class template?

Here is the actual class:

#include "StatusIteratorInterface.h"
#include "StatusContainer.h"
#include <map>

template<typename ITEM> class StatusContainer;

template <typename ITEM>
class StatusIterator : public StatusIteratorInterface<ITEM> {

public:
    StatusIterator( StatusContainer<ITEM>* container );

    ITEM* getFirst( void );
    ITEM* getNext( void );
    bool isDone( void );
    ITEM* getCurrent( void );

private:
    StatusContainer<ITEM>* container_;
    ITEM* currentItem_;
    std::multimap< std::pair<std::string, uint32_t>, ITEM >::iterator conIter1_; //gives error
    std::multimap< std::pair<string, uint32_t>, uint32_t>::iterator conIter_;
};

conIter_ is fine, but 'hard-codes' my template value. including the line above it gives this compile error:

/Users/joe/Developer/Template_DesignPattern_Iterator/StatusIterator.h:33: error: expected ';' before 'conIter1_'

I have a main that compiles and runs this code fine, but obviously is only suitable for uint32_t types. Pardon me if I've used incorrect terminology such as 'template argument' vice 'template parameter'.

+10  A: 

This is a common stumbling block in C++. Because ITEM is unknown when that class is parsed, the compiler can't deduce that your iterator really is a type.

Change this line:

std::multimap< std::pair<std::string, uint32_t>, ITEM >::iterator conIter1_;

to begin with typename, to show that it's a type:

typename std::multimap< std::pair<std::string, uint32_t>, ITEM >::iterator conIter1_;

You are creating a type that needs a template parameter which itself is already a template parameter. That is when you need to use typename.

Shmoopty