CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2000
    Posts
    101

    Function Pointers that Won't compile

    Help my class won't complile...


    typedef void (* TestFunc)();
    typedef std::map<int, TestFunc > FunctionMap ;


    class CMyClass
    {
    public:

    FunctionMap m_Map ;

    COracleObjectFactory()
    {
    m_FuncMap[1] = TestFunction; // Errors
    m_FuncMap[2] = TestFunction2; // Errors
    }

    void TestFunction()
    {
    // Some Instructions
    }

    void TestFunction2()
    {
    // Some Instructions
    }

    } ;






    I'm getting the following error

    error C2440: '=' : cannot convert from 'void (__thiscall CMyClass::*)(void)' to 'void (__cdecl *)(void)'
    There is no context in which this conversion is possible.

    All help grateful accepted...

    thanks in advance

    Martin


  2. #2
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Function Pointers that Won't compile

    You've defined TestFunc as a pointer to a global (non-member) function, and then tried to stick member functions in it. Either change TestFunction?() to static member functions, or change the typedef to:typedef void (* CMyClass::TestFunc)();



    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

  3. #3
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Function Pointers that Won't compile

    In your code:


    typedef void (* TestFunc)();




    you "create" a type of pointer to a function what takes NO params... Each NON-STATIC member function (like TestFunction or TestFunction2) have an implicit this parameter (used to access instance member data) - so In your case you can either use static member functions (but you wont be able to access member data from static function):


    #include <map>

    typedef void (CMyClass::* TestFunc)();
    typedef std::map<int, TestFunc > FunctionMap ;


    class CMyClass
    {
    public:

    FunctionMap m_Map;

    void COracleObjectFactory()
    {
    m_Map[1] = TestFunction; // Errors
    m_Map[2] = TestFunction2; // Errors
    }

    void TestFunction()
    {
    // Some Instructions
    }

    void TestFunction2()
    {
    // Some Instructions
    }

    } ;




    or use a pointers to a member functions:


    #include <map>

    typedef void (* TestFunc)();
    typedef std::map<int, TestFunc > FunctionMap ;


    class CMyClass
    {
    public:

    FunctionMap m_Map;

    void COracleObjectFactory()
    {
    m_Map[1] = TestFunction; // Errors
    m_Map[2] = TestFunction2; // Errors
    }

    static void TestFunction()
    {
    // Some Instructions
    }

    static void TestFunction2()
    {
    // Some Instructions
    }

    } ;







    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured