【问题标题】:C++ unordered_map causing compile-time errorC++ unordered_map 导致编译时错误
【发布时间】:2012-01-16 20:00:35
【问题描述】:

我有以下几点:

#include<iostream>
#include<unordered_map>
#include<tuple>

using namespace std;

class CTest {
    // Properties
    public:
        unordered_map<const string, tuple<int, int> > Layout;
    // Methods
    public:
        CTest ();
        ~CTest ();
};

CTest::CTest () {
    Layout["XYZ"] = make_tuple (0, 1);
}

CTest::~CTest () {
  // Do nothing
}

int main (int argc, char *argv[]) {
    CTest Test;
    return 0;
}

编译这个简单的程序会出现以下错误:

错误 C2678:二进制“==”:未找到采用“const std::string”类型的左侧操作数的运算符(或没有可接受的转换)

我在 Windows 7 中使用 Visual Studio 2010 Professional。

【问题讨论】:

    标签: c++ unordered-map


    【解决方案1】:

    除了把Layout改成:

    unordered_map<string, tuple<int, int> > Layout;
    

    正如 Johan 和 Benjamin 所说,您还需要 #include &lt;string&gt;

    注意,我不明白为什么需要更改为Layout,即使const 是多余的。

    【讨论】:

    • 感谢您的回复。抱歉,我的实际源代码中确实有 #include 。不幸的是,即使删除了 const,我也会遇到同样的错误。
    • @Shredderroy 不,不是cstring 只是string
    • 您能否用您的确切代码更新帖子,以防有其他差异?在我进行这些更改后,代码会在 VS2010 中为我编译(禁用扩展)。
    【解决方案2】:

    您需要删除键上的 const 限定符。

    unordered_map<const string, tuple<int, int> > Layout;
    

    进入

    unordered_map<string, tuple<int, int> > Layout;
    

    这是因为键总是 const,根据这个答案:

    Using a const key for unordered_map

    我认为根本原因与Duplicate const qualifier allowed in C but not in C++?有关

    另外,正如其他帖子指出的那样,您可能需要包含字符串(尽管 gcc 似乎与 iostream 一起提供)

    【讨论】:

    • 感谢您的链接。如上所述,即使删除了 const,编译也会失败,但我会阅读您的链接以获取更多提示。
    • Shredderroy 你的代码在 gcc4.7 c++11 下移除 const 后可以正常工作和编译。
    【解决方案3】:

    Visual Studio 2010 将按照您的#include &lt;string&gt; 编译您的源代码,以便它具有string 的比较测试可用。

    正如其他发帖人所提到的,您应该也将您的密钥设为常规的 string 而不是 const string,因为这符合 STL 标准,但这并不是绝对必要的make VS 2010 编译上面的源码。

    【讨论】:

    • 仅供参考,如果您在禁用扩展的情况下进行编译,除非您更改为 Layout,否则编译将失败。
    • 感谢您的回复。如前所述,即使删除了 const,编译也会失败。
    【解决方案4】:

    正如所指出的,您最初错误的原因是您需要包含&lt;string&gt;。但是,您可能会遇到另一个问题:

    unordered_map<const string, tuple<int, int> > Layout;
    

    您(可能)需要从该字符串中删除 const:

    unordered_map<string, tuple<int, int> > Layout;
    

    这在您的编译器上可能不是必需的,但它在我的编译器上。首先,const 是多余的,map/unordered_map 键无论如何都是 const,但这不是问题。问题与哈希函数模板不适用于 const 类型有关。

    下面的简单程序为我解决了问题:

    #include <functional>
    int main (int argc, char *argv[])
    {
        std::hash<const int> h;
        h(10);
    }
    

    http://ideone.com/k2vSy

    undefined reference to `std::hash<int const>::operator()(int) const'
    

    我目前无法解释。

    【讨论】:

    • @SethCarnegie:我不能。我认为这与不必要的原因没有任何关系,因为这个原因也适用于std::map。但是,map&lt;const string, tuple&lt;int, int&gt;&gt; 没有编译错误。我知道它与哈希函数有关,但我无法解释。
    • 好的,+1,感谢您的尝试。编辑:是的,我也没有得到任何编译错误。很奇怪。
    • 感谢您的回复。即使删除了 const,编译仍然失败,但我正在尝试使用此处的讨论进行进一步调查。
    • 本杰明,您的回复也包含我的问题的解决方案。赛斯是第一位的,所以我接受了。不过非常感谢。
    • 我在这里聚会迟到了,但图书馆只提供 std::hash 的特定专业。 std::hash 是其中之一, std::hash 不是。
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多