0

I have been trying to map a string to a function, but after testing and reading uncountable posts I still can't figure it out. I'm very new to C++, so excuse me if I say something weird here.

The error I'm getting is the following:

error: called object type 'void (test::*)()' is not a function or function pointer find->second(type);

The header has the mappings and the initializers, all of them are private.

        void printInfoHello();
        void printInfopHi();
        void prinInfoBye();

        std::map<std::string, void(test::*)()> infoMap = {
            {"hello", &test::printInfoHello},
            {"hi", &test::printInfopHi},
            {"bye", &test::prinInfoBye}
        };

Then in the cpp I tried to get the map to call the functions based on the string, which seems to work just fine when I pass something that doesn't exist in the mapping, but if I type "hi" it returns the error I mentioned above.


void test::processInput(std::string &input)
{
    std::vector<std::string> val = explodeVals::split(input, ' ');

    if (val.empty()) {
        std::cout << "Empty input!" << std::endl;
        return;
    }

    std::string type = val[0];
    std::cout << "Is it taking the right type: " type << std::endl;


    auto find = infoMap.find(type);
    if (find == infoMap.end()) {
         std::cout << "Error, input not found" << std::endl;
         return;
    }
    find->second(type);
}


void printInfoHello(){
    std::cout << "Saying hello!" << std::endl;
}

void printInfoHi(){
    std::cout << "Saying hi!" << std::endl;
}


void printInfoBye(){
    std::cout << "Saying good bye!" << std::endl;
}

Also, on a side note, I will need to extend this and some functions are going to receive inputs, like a vector (std::vectorstd::string &Input) or strings.

I'm also including the following libraries in both the .h and .cpp files.

#include <iostream>
#include <vector>
#include <map>
#include <functional>
  • `void (test::*)()` is a pointer to member function, you need to use the correct syntax to call a function through such a pointer. However it's very unclear what you are trying to do with `find->second(type);` since the functions don't accept a `std::string` argument – UnholySheep Jun 08 '22 at 11:46
  • 1
    `(this->*(find->second))();` – Jarod42 Jun 08 '22 at 11:48
  • See [Pointers_to_member_function](https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_member_functions). – Jarod42 Jun 08 '22 at 11:50
  • Hi @Jarod42, thank you! This was exactly what I needed help with to get working. Would you mind giving me some explanation about the bit this->*? – noClueAboutWhatIMDoing Jun 08 '22 at 18:17
  • Hi @UnholySheep, ok, that makes sense! And this helped me to implement the other mappings that receive values. – noClueAboutWhatIMDoing Jun 08 '22 at 18:18
  • 1
    `.*` and `->*` are the way to call pointer of member function (see doc link I gave you too). c++17 has [`std::invoke`](https://en.cppreference.com/w/cpp/utility/functional/invoke) to have simpler syntax. – Jarod42 Jun 08 '22 at 18:43

0 Answers0