2

Here is a code snippet from my project:

template<typename Second, typename First = const UINT64>
class Event : virtual public id_manager<>
{
    friend class EventHandler;
    typedef std::map<First, EventHandler> eventMap;
    static eventMap mapper;
    static eventMap StartMapping()
    {
        eventMap temp;
        return temp;
    }
public:
    Event(){}
    void operator+=(EventHandler _handler)
    {
        mapper[this->getID()] = _handler;
    }
};
// INITIALIZATION FAILED HERE:
template<typename Second, typename First = const UINT64>
Event<Second, First>::eventMap Event<Second, First>::mapper(Event<Second, First>::StartMapping());

Here is output error from Visual studio 2010:

Warning 1 warning C4346: 'Event::eventMap' : dependent name is not a type c:\users\admin\documents\visual studio 2010\projects\cppsystem\cppsystem\main.cpp 67 Error 2 error C2143: syntax error : missing ';' before 'Event::mapper' c:\users\admin\documents\visual studio 2010\projects\cppsystem\cppsystem\main.cpp 67 Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\admin\documents\visual studio 2010\projects\cppsystem\cppsystem\main.cpp 67 Error 4 error C1903: unable to recover from previous error(s); stopping compilation c:\users\admin\documents\visual studio 2010\projects\cppsystem\cppsystem\main.cpp 67

I hope this pice of code is enough. I can't initialize static map member using "initialization functin" for that map. thanks alot!

thiton
  • 34,333
  • 3
  • 63
  • 96
codekiddy
  • 5,217
  • 9
  • 46
  • 73
  • [Where and why do I have to put the "template" and "typename" keywords?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Xeo Jan 03 '12 at 09:36
  • Hey thank you for that useful link... I'm reading that post from strech now! cheers my friend. – codekiddy Jan 03 '12 at 09:50

1 Answers1

5

You need typename to tell the compiler that the eventMap is a type.

template<typename Second, typename First = const UINT64>
typename Event<Second, First>::eventMap Event<Second, First>::mapper(Event<Second, First>::StartMapping());

See The "typename" keyword

Community
  • 1
  • 1
ronag
  • 43,567
  • 23
  • 113
  • 204
  • thanks alot, umn ?! lol I didn't know that. I'm prety new to templates. this class template now works just fine... now have to solve other classes :D cheers! – codekiddy Jan 03 '12 at 09:48