Reputation: 33
I set up this account mainly because I couldn't find answers elsewhere. I checked, various tutorials or questions/ answers on stackoverflow and different pages.
I'm programming a terminal based textadventure and need a map of functions. This is what I got (I left out all the stuff that isn't of interesting for the problem)
#include <map>
using namespace std;
class CPlayer
{
private:
//Players functions:
typedef void(CPlayer::*m_PlayerFunction)(void); //Function-pointer points to various player
//functions
map<char*, m_PlayerFunction> *m_FunctionMap; //Map containing all player functions
public:
//Constructor
CPlayer(char* chName, CRoom* curRoom, CInventory* Inventory);
//Functions:
bool useFunction(char* chPlayerCommand);
void showDoors(); //Function displaing all doors in the room
void showPeople(); //Function displaying all people in the room
};
#endif
#include "CPlayer.h"
#include <iostream>
CPlayer::CPlayer(char chName[128], CRoom* curRoom, CInventory *Inventory)
{
//Players functions
m_FunctionMap = new map<char*, CPlayer::m_PlayerFunction>;
m_FunctionMap->insert(std::make_pair((char*)"show doors", &CPlayer::showDoors));
m_FunctionMap->insert(std::make_pair((char*)"show people", &CPlayer::showPeople));
}
//Functions
//useFunction, calls fitting function, return "false", when no function ist found
bool CPlayer::useFunction(char* chPlayerCommand)
{
CFunctions F;
map<char*, m_PlayerFunction>::iterator it = m_FunctionMap->begin();
for(it; it!=m_FunctionMap->end(); it++)
{
if(F.compare(chPlayerCommand, it->first) == true)
{
cout << "Hallo" << endl;
(it->*second)();
}
}
return false;
}
Now, the problem is the following:
If I call the function this way:
(it->*second)();
which seems to be how it is supposed to be done, I get the following error:
error: ‘second’ was not declared in this scope
If I call the function this way:
(*it->second)();
which is what I got from this thread: Using a STL map of function pointers, I get following error:
error: invalid use of unary ‘ * ’ on pointer to member
I'd be very glad if someone could help me. Thanks ahead for all the upcoming answers.
PS: It would also be interesting to know whether "map" or "unordered_map" is the better way of solving this problem.
As I said, thanks ahead: GB
Upvotes: 3
Views: 2580
Reputation: 7279
The difficulty may be that it is at the same time a map, and it involves pointer-to-members, which makes the syntax to invoke more complicated with lots of parentheses that must be at the right positions. I think it should be something like this:
(this->*(it->second))()
Alternatively, as Rakete1111 points out, the following works as well:
(this->*it->second)()
(Note that the latter is less verbose, but also less easy to read for people who do not have the operator precedence on the top of their minds).
Upvotes: 2