views:

138

answers:

3

Hi all;

My C++ is a little rusty having worked in Java and C# for the last half dozen years. I've got a stupid little error that I just cannot figure out.

I've pared the code down as much as possible.

#include <list>
template<class T> class Subscriber
{
    virtual void published( T t ) = 0;
};

template <class T> class PubSub
{
private:
    std::list< Subscriber<T>* > subscribers;
public:
    void publish( T t );
};

template<class T> void PubSub<T>::publish( T t ) 
{
    for( std::list< Subscriber<T>* >::iterator i = subscribers.begin(); i != subscribers.end(); ++i )
        i->published( t );
}

When I try and compile this (by including this header file in a code file), I get the following error:

../util/pubsub.h: In member function ‘void PubSub<T>::publish(T)’:
../util/pubsub.h:18: error: expected `;' before ‘i’
../util/pubsub.h:18: error: ‘i’ was not declared in this scope

What am I missing here?

+5  A: 

for( typename std::list< Subscriber* >::iterator i = ...

Marsh Ray
Thanks. Worked perfectly.
Andrew
+3  A: 

This

std::list< Subscriber<T>* >::iterator

needs to be this

typename std::list< Subscriber<T>* >::iterator

The compiler assumes nested names in templates are static variables (not types) until told otherwise.

Tyler McHenry
+5  A: 
for( typename std::list< Subscriber<T>* >::iterator i = subscribers.begin(); i != subscribers.end(); ++i )

You need the typename because iterator is a dependent name. The compiler has to check the template type T before it knows whether iterator is a type or a value. In those cases, it assumes it to be a value, unless you add typename.

jalf