简体   繁体   中英

c++ template syntax error

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?

for( typename std::list< Subscriber<T>* >::iterator i = ...
     ^^^^^^^^
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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM