Reputation: 12009
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?
Upvotes: 2
Views: 938
Reputation: 2875
for( typename std::list< Subscriber<T>* >::iterator i = ...
^^^^^^^^
Upvotes: 6
Reputation: 248139
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
.
Upvotes: 6
Reputation: 76730
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.
Upvotes: 4