CMapStringToPtr with ptrs to member functions

My problem can be illustrated clearly with this example.

Let us say I have this class definition:

class TEST
{ public:

  TEST ();
  ~TEST ();
  CMapStringToPtr m_FunctionMap;
  void Function (void);

{ m_FunctionMap.SetAt ("Testing", Function); }


void TEST::Function (void)
{ return; }

How can I modify the:
m_FunctionMap.SetAt ("Testing", Function);

so that given the "Testing" string, the CMap will give a pointer to this member function?

Thank you
Who is Participating?
Oliver_DornaufConnect With a Mentor Commented:
#include <afxtempl.h> to get the definition of CMap.
I guess it wont work because Function is not a static member.

Regards Gus
Pointer to non static memberfunctions is tricky !

class test {
 typedef void (test::*pmf)();
  //signature of the memberfunctions
  test() {
    m_FuntionMap.SetAt (CString("fkt1"), &test::fkt1);
    m_FuntionMap.SetAt (CString("fkt2"), &test::fkt2);
  ~test() {;}

    void fkt1();
      // must same signature as pmf
    void fkt2();

    CMap<LPCSTR,LPCSTR, pmf, pmf> m_FuntionMap;

test* myTest = new test();
CString string("fkt1");

test::pmf funtionPtr;
myTest->m_FuntionMap.Lookup (string, funtionPtr);




 // typedef int (test::*pmf)(int, bool)

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

IgorStravinskyAuthor Commented:

I am having a problem with your code.  Here is my implementation (the complete code):


// test.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "test.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

class test
{ public:
  typedef void (test::*pmf) ();
  //signature of the memberfunctions

  test ()
  { m_FunctionMap.SetAt (CString ("fkt1"), &test::fkt1);
    m_FunctionMap.SetAt (CString ("fkt2"), &test::fkt2);

  ~test() {; }

  void fkt1 ();
  void fkt2 ();

  CMap<LPCSTR, LPCSTR, pmf, pmf> m_FunctionMap;

// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  int nRetCode = 0;

  // initialize MFC and print and error on failure
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    // TODO: change error code to suit your needs
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    nRetCode = 1;
    // TODO: code your application's behavior here.
    CString strHello;
    cout << (LPCTSTR)strHello << endl;

  test* myTest = new test();
  CString string("fkt1");

  test::pmf functionPtr;
  myTest->m_FunctionMap.Lookup (string, functionPtr);


  return nRetCode;


I get these compiler errors

F:\test\test.cpp(28) : error C2143: syntax error : missing ';' before '<'
F:\test\test.cpp(28) : error C2501: 'CMap' : missing storage-class or type specifiers
F:\test\test.cpp(28) : error C2059: syntax error : '<'
F:\test\test.cpp(28) : error C2238: unexpected token(s) preceding ';'
F:\test\test.cpp(61) : error C2039: 'm_FunctionMap' : is not a member of 'test'
        F:\test\test.cpp(14) : see declaration of 'test'
F:\test\test.cpp(61) : error C2228: left of '.Lookup' must have class/struct/union type

Line 28 is:
CMap<LPCSTR, LPCSTR, pmf, pmf> m_FunctionMap;

What is wrong with this statement?  What should I do to fix this problem?
IgorStravinskyAuthor Commented:
I am going to reject your answer until you or someone else can fix the problem.  Thanks.
IgorStravinskyAuthor Commented:
It is always the simple things that cause the problems. =o)
All Courses

From novice to tech pro — start learning today.