c++:无法将 pair<int、MLine*> 插入到unordered_map

c++: Can't insert a pair<int, MLine*> to an unordered_map

本文关键字:gt 插入 map unordered MLine int pair c++ lt      更新时间:2023-10-16

我的my_map定义为:

 std::unordered_map<MyAction, MyLine * >;

(myaction是枚举类)

myline为 std::vector<MyPoint>;

和mylines是std::vector<MyLine>;

然后使用以下代码:

for (const auto &myline : mylines) {
            my_map.insert(
                std::pair<MyAction, const Myline *>(MyAction::KEEP_1, &myline));

我得到以下错误:

my_utility.cpp:85:33: error: no matching member function for call to 'insert'
            my_map.insert(
            ~~~~~~~~~~~~~~~~~~~~^~~~~~
my_project/external/clang/darwin/include/c++/v1/unordered_map:909:26: note: candidate function not viable: no known conversion from 'pair<my_namespace::MyAction, const Myline *>' to 'const pair<const std::__1::unordered_map<my_namespace::MyAction, std::__1::vector<math::Vector3<double>, std::__1::allocator<math::Vector3<double> > > *, std::__1::hash<my_namespace::MyAction>, std::__1::equal_to<my_namespace::MyAction>, std::__1::allocator<std::__1::pair<const MyAction, std::__1::vector<math::Vector3<double>, std::__1::allocator<math::Vector3<double> > > *> > >::key_type, std::__1::unordered_map<my_namespace::MyAction, std::__1::vector<math::Vector3<double>, std::__1::allocator<math::Vector3<double> > > *, std::__1::hash<my_namespace::MyAction>, std::__1::equal_to<my_namespace::MyAction>, std::__1::allocator<std::__1::pair<const MyAction, std::__1::vector<math::Vector3<double>, std::__1::allocator<math::Vector3<double> > > *> > >::mapped_type>' for 1st argument
    pair<iterator, bool> insert(const value_type& __x)
                         ^

知道怎么了?谢谢!

您的地图具有MyLine *,但在插入功能中,您还编写了const。删除它,它应该起作用。