C++ unordered_map 导致编译时错误
我有以下内容:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了将
Layout
更改为:如 Johan 和 Benjamin 所说,您还需要
#include
。注意,我不明白为什么需要更改
Layout
,即使const
是多余的。In addition to changing
Layout
to: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 theconst
is superfluous.您需要删除键上的 const 限定符。
这
是因为根据这个答案,键始终是常量:
使用常量键对于 unordered_map
我认为根本原因与 C 中允许重复 const 限定符,但 C++ 中不允许?
另外,正如其他帖子指出的那样,您可能需要包含字符串(尽管对于 gcc,它似乎与 iostream 一起提供)
You need to remove the const qualifier on the key.
into
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)
只要您
#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 forstring
available.As the other posters mention, you should also make your key a regular
string
rather than aconst string
, as this is conformant to the STL standard, but this is not strictly necessary to make VS 2010 compile the source above.正如所指出的,最初错误的原因是您需要包含
。但是,您可能会遇到另一个问题:您(可能)需要从该字符串中删除 const:
这在您的编译器上可能不是必需的,但在我的编译器上是必需的。首先,const是多余的,map/unordered_map键无论如何都是const,但这不是问题。该问题与哈希函数模板不适用于 const 类型有关。
以下简单的程序为我隔离了问题:
http://ideone.com/k2vSy
目前我不能,解释一下这一点。
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:You (may) need to remove the const from that string:
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:
http://ideone.com/k2vSy
I cannot, at present time, explain this.