C++映射字符串函数返回错误

发布于 2025-02-05 14:40:04 字数 1718 浏览 3 评论 0原文

我一直在尝试将字符串映射到函数,但是在测试和读取无数帖子后,我仍然无法弄清楚。我是C ++的新手,所以请原谅我在这里说些奇怪的话。

我遇到的错误是:

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

标题具有映射和初始化器,所有这些都是私人的。

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

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

然后,在CPP中,我试图获取地图以基于字符串调用功能,当我传递映射中不存在的东西时,它似乎很好,但是如果我键入“ HI”,它将返回错误i i上面提到的。


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;
}

另外,从附带说明,我将需要扩展此功能,并且某些功能将接收输入,例如向量(std :: vectorstd :: string&amp; input)或字符串。

我还在.h和.cpp文件中包括以下库。

#include <iostream>
#include <vector>
#include <map>
#include <functional>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文