我在将结构添加到 map 时遇到问题。我真的不明白这个错误。
有两个错误:
- 我无法声明“struct”类型的映射
- 我无法使用 insert 将我的结构“插入”到 map 中
我做错了什么?
#include <iostream>
#include <map>
int main()
{
typedef struct
{
std::string stringVar;
unsigned unsignedVar;
float floatVar;
} MyTypeDefStruct;
MyTypeDefStruct myTypeDefStruct;
myTypeDefStruct.stringVar = "myStr";
myTypeDefStruct.unsignedVar = 1000;
myTypeDefStruct.floatVar = -10.0;
float anotherFloat = -20.0;
std::map<MyTypeDefStruct, float> myMap;
myMap.insert(std::pair<MyTypeDefStruct, float> (myTypeDefStruct, anotherFloat));
return 0;
}
错误:
test.cpp: In function 'int main()':
test.cpp:21:36: error: template argument for 'template<class _Tp> struct std::less' uses local type 'main()::MyTypeDefStruct'
std::map<MyTypeDefStruct, float> myMap;
^
test.cpp:21:36: error: trying to instantiate 'template<class _Tp> struct std::less'
test.cpp:21:36: error: template argument 3 is invalid
test.cpp:21:36: error: template argument for 'template<class _T1, class _T2> struct std::pair' uses local type 'const main()::MyTypeDefStruct'
test.cpp:21:36: error: trying to instantiate 'template<class _T1, class _T2> struct std::pair'
test.cpp:21:36: error: template argument 4 is invalid
test.cpp:21:43: error: invalid type in declaration before ';' token
std::map<MyTypeDefStruct, float> myMap;
^
test.cpp:23:11: error: request for member 'insert' in 'myMap', which is of non-class type 'int'
myMap.insert(std::pair<MyTypeDefStruct, float> (myTypeDefStruct, anotherFloat));
^
test.cpp:23:50: error: template argument for 'template<class _T1, class _T2> struct std::pair' uses local type 'main()::MyTypeDefStruct'
myMap.insert(std::pair<MyTypeDefStruct, float> (myTypeDefStruct, anotherFloat));
^
test.cpp:23:50: error: trying to instantiate 'template<class _T1, class _T2> struct std::pair'
最佳答案
您的代码中存在几个问题:
- 该结构应该在函数 main 之外定义,在这种情况下看不出为什么要在那里定义它,并且由于它在 main 内部是本地的,所以您不能在其他任何地方使用它!<
- 您正在使用 C 风格
typedef struct ... StructName
而不是简单地使用struct MyStruct
. - 您的结构没有实现
operator<
,因此,因为std::map
是一个有序映射,无论如何它都不需要比较两个键(这里,键是你的结构),它不能插入任何对。 - 您的结构没有实现
operator==
这对于实际检索某个键的值很重要...您应该能够检查这些键是否相似。 - 另一个小修复 - 使用
std::make_pair
而不是它的ctor。
您的代码应如下所示:
#include <iostream>
#include <map>
struct MyStruct
{
std::string stringVar;
unsigned unsignedVar;
float floatVar;
friend bool operator<(const MyStruct& l, const MyStruct& r)
{
return std::tie(l.stringVar, l.unsignedVar, l.floatVar)
< std::tie(r.stringVar, r.unsignedVar, r.floatVar);
}
friend bool operator==(const MyStruct& l, const MyStruct& r)
{
return std::tie(l.stringVar, l.unsignedVar, l.floatVar)
== std::tie(r.stringVar, r.unsignedVar, r.floatVar);
}
};
int main()
{
MyStruct my_struct;
my_struct.stringVar = "myStr";
my_struct.unsignedVar = 1000;
my_struct.floatVar = -10.0;
float anotherFloat = -20.0;
std::map<MyStruct, float> myMap;
myMap.insert(std::make_pair(my_struct, anotherFloat));
return 0;
}
我是如何确定第一个问题的?在第一个错误中(总是看第一个错误,其他的可能会误导!),它是这样写的:
test.cpp:21:36: error: template argument for 'template<class _Tp> struct std::less' uses local type 'main()::MyTypeDefStruct'
std::map<MyTypeDefStruct, float> myMap;
注意这里的模板初始化使用的是main()::MyTypeDefStruct
而不是 MyTypeDefStruct
!!!这些类型不同,第一种类型仅在 main()
的范围可用。 !
第三点是因为当你修正了第一点,你会得到:
error: no match for 'operator<' (operand types are 'const MyStruct' and 'const MyStruct')
关于c++ - 将结构添加到映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62735210/