【问题标题】:could not convert ... from '<brace-enclosed initializer list>' to map无法将 ... 从 '<brace-enclosed initializer list>' 转换为 map
【发布时间】:2015-08-25 17:19:10
【问题描述】:

我现在正在尝试修复特定故障大约两个小时,并且我已经完成了我的知识。有什么想法吗?

失败:

could not convert '{{TEST1, (& ffm::cond_test1)}, {TEST2, (& ffm::cond_test2)}}' from '<brace-enclosed initializer list>' to 'std::map<ffm::conditions, bool (*)()>'

代码:

#include <map>
#include <iostream>

namespace ffm {
    typedef bool (*condFunction)();

    enum conditions {
        TEST1 = 0,
        TEST2
    };

    bool cond_test1();
    bool cond_test2();
}

using namespace ffm;

bool cond_test1()
{
    std::cout << "cond_test1 is always TRUE" << std::endl;
    return true;
}

bool cond_test2()
{
    std::cout << "cond_test2 is always FALSE" << std::endl;
    return false;
}

int main() {
    std::map<conditions, condFunction> condFuncMap = { 
        {conditions::TEST1, &cond_test1}, 
        {conditions::TEST2, &cond_test2} };

}

我正在使用 gcc4.8.4

【问题讨论】:

  • using 指令并不意味着后面的所有内容都添加为该命名空间的成员,它只是将该命名空间内的名称带入范围。将您的函数定义为bool ffm::cond_test1() { ... }

标签: c++ c++11 dictionary


【解决方案1】:

您需要将您的函数定义为以ffm:: 开头。这是因为您的using 指令仅意味着可以在不使用ffm:: 的情况下调用cond_test1/cond_test2。如果你不把ffm::放在正确的定义之前,它们就像函数的更多重载(::cond_test1::cond_test2),编译器将无法判断你想要哪个重载ffm::cond_test1或@ 987654330@作为命名空间不需要命名)

    bool ffm::cond_test1()
    {
        std::cout << "cond_test1 is always TRUE" << std::endl;
        return true;
    }

    bool ffm::cond_test2()
    {
        std::cout << "cond_test2 is always FALSE" << std::endl;
        return false;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    相关资源
    最近更新 更多