Hi guys,
Could you please help me to correct this example code ?
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
#include <string>
#include <map>
class HiddenFunc
{
public:
HiddenFunc();
~HiddenFunc();
void execute();
private:
int hello();
std::map<std::string, int (HiddenFunc::*)()> funcs;
};
HiddenFunc::HiddenFunc()
{
funcs["hello"] = &HiddenFunc::hello;
}
int
HiddenFunc::hello()
{
return 10;
}
void
HiddenFunc::execute()
{
std::cout
<< "Value : " << this->*funcs["hello"]() << std::endl; // wrong line (see below)
}
int main()
{
HiddenFunc hidden_func;
hidden_func.execute();
return 0;
}
|
---
The compiler message is:
g++ hiddenfunc.cc -o hiddenfunc
hiddenfunc.cc: In member function ‘void HiddenFunc::execute()’:
hiddenfunc.cc:34:46: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((HiddenFunc*)this)->HiddenFunc::funcs.std::map<_Key, _Tp, _Compare, _Alloc>::operator[]<std::basic_string<char>, int (HiddenFunc::*)(), std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, int (HiddenFunc::*)()> > >((* & std::basic_string<char>(((const char*)"hello"), (*(const std::allocator<char>*)(& std::allocator<char>()))))) (...)’, e.g. ‘(... ->* ((HiddenFunc*)this)->HiddenFunc::funcs.std::map<_Key, _Tp, _Compare, _Alloc>::operator[]<std::basic_string<char>, int (HiddenFunc::*)(), std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, int (HiddenFunc::*)()> > >((* & std::basic_string<char>(((const char*)"hello"), (*(const std::allocator<char>*)(& std::allocator<char>())))))) (...)’
make: *** [hiddenfunc] Error 1
---
So, How should I call the method ??
Thanks,