Passing Function Pointer as argument to Member Function of the class

Hi all

Here I need to pass an function-pointer as an argument to a function which is pointing to the non-static member function of the class. I am using VS-2008 in Windows.

1
2
3
4
5
6
7
8
9
10
11
12
13
//Header file - Declaring Class
class Class_Func_Ptr
{
	public:
		Class_Func_Ptr();
		virtual ~Class_Func_Ptr();

		void __thiscall func_1(std::string str);

		std::map<std::string, void (Class_Func_Ptr::*)(std::string)> func_list;

		void store_func_map(std::string str_name, void (Class_Func_Ptr::*)(std::string));		
};


Now in .cpp file I had definitions as following and getting and compile time error message:


error C2298: '=' : illegal operation on pointer to member function expression


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Class_Func_Ptr::Class_Func_Ptr()
{
	store_func_map("abc", &Class_Func_Ptr::func_1);
}

Class_Func_Ptr::~Class_Func_Ptr()
{
}

//Function has an argument as pointer to the non-static class member function.
//To store the pointed function within a std::Map.

void Class_Func_Ptr::store_func_map(std::string str_name, void (Class_Func_Ptr::*func_ptr)(std::string))
{
	func_list[str_name.c_str()] = (*this.*func_ptr);
}


I will be very much thankful for the guidance, where I am doing wrong and how can I visualize the problem for better understanding.
func_list[str_name] = func_ptr;
Thanks for reply. After making changes as you suggested i.e.

1
2
3
4
void Class_Func_Ptr::store_func_map(std::string str_name, void (Class_Func_Ptr::*func_ptr)(std::string))
{
	func_list[str_name.c_str()] = (func_ptr);
}


I am getting some error like:

1
2
3
4
5
6
7
8
9
10
11
//-------------------------------------------------
//3 different errors are getting repeated
//-------------------------------------------------

error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'

error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'

 Total - 17 error(s), 0 warning(s)


and here I am unable to understand the errors ...
Last edited on
Hi Thanks all

I was missing a header - file. i.e.

#include <string>
Topic archived. No new replies allowed.