#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    44
    Rep Power
    10

    Template issue (with signals and slots)


    I'm interested in an article I saw at code project (http://www.codeproject.com/KB/cpp/CppEvents.aspx) but I get compilation issues with g++ that I don't know how to solve. Unfortunately my knowledge of templates and why they would cause such issues is beyond me, hence why I'm here.

    The error is relating to what the std::map contains, which is more templated code.

    sigslot_test.h: In member function ‘bool CppEvent1<ReturnT, ParamT>::detach(CppEventHandler)’:
    sigslot_test.h:58: error: expected `;' before ‘it’


    The code looks like the following.

    Code:
    #include <iostream>
    #include <map>
    using namespace std;
    
    typedef  int CppEventHandler;
    
    template <typename ReturnT, typename ParamT>
    class EventHandlerBase1
    {
    public:
      virtual ReturnT notify(ParamT param) = 0;
    };
    
    template <typename ListenerT, typename ReturnT, typename ParamT>
    class EventHandler1 : public EventHandlerBase1<ReturnT, ParamT>
    {
      typedef ReturnT (ListenerT::*PtrMember)(ParamT);
      ListenerT* mObject;
      PtrMember mMember;
      
    public:
      EventHandler1(ListenerT* object, PtrMember member) : mObject(object), mMember(member)
      {
      
      }
      
      ReturnT notify(ParamT param)
      {
        return (mObject->*mMember)(param);
      }
    };
    
    
    template <typename ReturnT, typename ParamT>
    class CppEvent1
    {
      typedef std::map<int, EventHandlerBase1<ReturnT, ParamT> *> HandlersMap;
      HandlersMap mHandlers;
      int mCount;
      
    public:
      CppEvent1() : mCount(0)
      {
      }
      
      template <typename ListenerT>
      CppEventHandler attach(ListenerT* object, ReturnT (ListenerT::*member)(ParamT))
      {
        typedef ReturnT (ListenerT::*PtrMember)(ParamT);
        mHandlers[mCount] = (new EventHandler1<ListenerT, ReturnT, ParamT>(object, member));
        mCount++;
        return mCount-1;
      }
      
      bool detach(CppEventHandler id)
      {
        HandlersMap::iterator it = mHandlers.find(id);
        if (it == mHandlers.end())
          return false;
          
        delete it->second;
        mHandlers.erase(it);
        return true;
      }
      
      ReturnT notify(ParamT param)
      {
        HandlersMap::iterator it = mHandlers.begin();
        for (; it != mHandlers.end(); it++)
        {
          it->second->notify(param);
        }
        return true;
      }
     
      
    };
    
    int main()
    {
      cout << "hello and welcome to yet another coding adventure\n";
    }
    It seems like g++ doesn't like
    typedef std::map<int, EventHandlerBase1<ReturnT, ParamT> *> HandlersMap;
    but I don't know why.

    Cheers for any help

    In case anyone feels they are seeing double, I've also posted this question to gamedev.net
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    I think you just need to use the typename keyword, as in:
    Code:
      bool detach(CppEventHandler id)
      {
        typename HandlersMap::iterator it = mHandlers.find(id);
        ...
    (since HandlersMap::iterator is a type that depends on your template parameters and thus cannot be resolved when the template definition is parsed). This might shed some light: http://www.gotw.ca/gotw/035.htm

    In case anyone feels they are seeing double, I've also posted this question to gamedev.net
    Thanks for the disclosure. You should actually post a link to your other post, but at least you tried.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    44
    Rep Power
    10
    Thank you Lux Perpetua, that is it exactly.

    From the solution you've given me, it appears I've been made guilty of not realising the similarity between this issue and my original question to gamedev (the link is here) of which gave me a solution to windows compilation issues.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo