2

I am working on a template Subject class I will use as a base class for some event-driven programming. Currently, I am working on callback registration and I received "Template argument for template type parameter must be a type; did you forget 'typename'?" at the line of std::map.

I feel that the answer is related with this link, but I could not form it to solve my problem.

Here's the header of Subject class:

#ifndef Subject_hpp
#define Subject_hpp

#include "Observer.hpp"

#include <list>
#include <map>

template<class T>
class Subject
{
public:
    Subject();
    virtual ~Subject();

    virtual void attach( Observer* const object, void(Observer::* const func)(T) );
    virtual void detach(const int pObserverId);
    virtual void notify(T& pEvent);

    // Clear the subject: Clear the attached observer list
    void clear();

private:


    std::list< std::function<void(T)> > mObserverList;

    // For a better callback management in case they will
    // be required to remove before the application is terminated
    std::map<int, std::list< std::function<void(T)> >::iterator> mObserverRegister; // Throws: Template argument for template type parameter must be a type; did you forget 'typename'?

};

#endif /* Subject_hpp */

T in here will be a user-defined event class such as MouseEvent, KeyboardEvent, etc.

ciyo
  • 725
  • 4
  • 16
  • 36
  • 3
    Long story short, `std::list< std::function >::iterator` is a dependent name and needs to be qualified with `typename`. – NathanOliver Mar 19 '18 at 19:02
  • Hello Nathan! I respect your effort, but your approach actually does not help people who try to improve their abilities. Your comment is also ambiguous and does not provide enough insight to solve the problem. I would suggest you to take @Christian Hackl as an example to actually help people. Thank you and have a nice day! – ciyo Mar 19 '18 at 19:10

1 Answers1

8

You are missing a typename. It should be:

std::map<int, typename std::list< std::function<void(T)> >::iterator>
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62