Click to See Complete Forum and Search --> : Function Pointers that Won't compile
JMK
November 27th, 2001, 07:19 AM
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
James Curran
November 27th, 2001, 07:53 AM
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.
Igor Soukhov
November 27th, 2001, 07:56 AM
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)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com
Russian Software Developer Network http://rsdn.ru
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.