C++ unordered_map 导致编译时错误

发布于 2024-12-27 03:03:36 字数 679 浏览 1 评论 0原文

我有以下内容:

#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。

I have the following:

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

Compiling this simple programme gives the following error:

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)

I'm using Visual Studio 2010 Professional in Windows 7.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁与争疯 2025-01-03 03:03:36

除了将 Layout 更改为:

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

如 Johan 和 Benjamin 所说,您还需要 #include

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

In addition to changing Layout to:

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

as stated by Johan and Benjamin you also need to #include <string>.

Note, I do not understand why the change to Layout is required, even if the const is superfluous.

转身泪倾城 2025-01-03 03:03:36

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

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

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

是因为根据这个答案,键始终是常量:

使用常量键对于 unordered_map

我认为根本原因与 C 中允许重复 const 限定符,但 C++ 中不允许?

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

You need to remove the const qualifier on the key.

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

into

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

this is because keys are always const, according to this answer:

Using a const key for unordered_map

I figure the underlying reason is related to Duplicate const qualifier allowed in C but not in C++?

Also, as other posts pointed out you may need to include string (although with gcc it seems to come with iostream)

淡笑忘祈一世凡恋 2025-01-03 03:03:36

只要您#include,Visual Studio 2010 就会按原样编译您的源代码,以便它可以进行string 的比较测试。

正如其他发帖者提到的,您应该还使您的密钥成为常规字符串而不是常量字符串,因为这符合STL标准,但这并不是让 VS 2010 编译上面的源代码所必需的。

Visual Studio 2010 will compile your source code as is as long as you #include <string> so that it has the comparison tests for string available.

As the other posters mention, you should also make your key a regular string rather than a const string, as this is conformant to the STL standard, but this is not strictly necessary to make VS 2010 compile the source above.

违心° 2025-01-03 03:03:36

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

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'

目前我不能,解释一下这一点。

As was pointed out, the reason for your initial error was that you needed to include <string>. However, you may have another problem with this:

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

You (may) need to remove the const from that string:

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

This may not be necessary on your compiler, but it is on mine. First of all, the const is superfluous, map/unordered_map keys are const anyway, but that is not the problem. The problem has to do with the hash function template not working for const types.

The following simple program isolates the problem for me:

#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'

I cannot, at present time, explain this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文