【问题标题】:Adding a reference to an object to a map in C++ [duplicate]将对象的引用添加到C ++中的地图[重复]
【发布时间】:2017-03-29 20:01:00
【问题描述】:

我在 C++ 中将对象的引用插入到地图中时遇到问题。

这里有一些示例代码来展示我想要做什么:

#include <iostream>
#include <map>

class Foo {
public:
    // ...

    void addToMap();
};

std::map<std::string, Foo&> myMap;

void Foo::addToMap() {
    myMap.insert(std::make_pair(std::string("hello"), *this));
}

int main() {
    Foo foo;
    foo.addToMap();
}

当我编译时,我收到一条非常神秘的错误消息

main.cpp: In member function ‘void Foo::addToMap()’:
main.cpp:12:48: error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, Foo&>::insert(std::pair<const char*, Foo>)’
     myMap.insert(std::make_pair("hello", *this));
                                                ^
In file included from /usr/include/c++/6.3.1/map:61:0,
                 from main.cpp:2:
/usr/include/c++/6.3.1/bits/stl_map.h:731:7: note: candidate: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = Foo&; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const std::__cxx11::basic_string<char>, Foo&>]
       insert(const value_type& __x)
       ^~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:731:7: note:   no known conversion for argument 1 from ‘std::pair<const char*, Foo>’ to ‘const value_type& {aka const std::pair<const std::__cxx11::basic_string<char>, Foo&>&}’
/usr/include/c++/6.3.1/bits/stl_map.h:739:9: note: candidate: template<class _Pair, class> std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = _Pair; <template-parameter-2-2> = <template-parameter-1-2>; _Key = std::__cxx11::basic_string<char>; _Tp = Foo&; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >]
         insert(_Pair&& __x)
         ^~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:739:9: note:   template argument deduction/substitution failed:
/usr/include/c++/6.3.1/bits/stl_map.h:735:32: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
       template<typename _Pair, typename = typename
                                ^~~~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:752:7: note: candidate: void std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::initializer_list<std::pair<const _Key, _Tp> >) [with _Key = std::__cxx11::basic_string<char>; _Tp = Foo&; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >]
       insert(std::initializer_list<value_type> __list)
       ^~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:752:7: note:   no known conversion for argument 1 from ‘std::pair<const char*, Foo>’ to ‘std::initializer_list<std::pair<const std::__cxx11::basic_string<char>, Foo&> >’
/usr/include/c++/6.3.1/bits/stl_map.h:781:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = Foo&; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const std::__cxx11::basic_string<char>, Foo&>]
       insert(const_iterator __position, const value_type& __x)
       ^~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:781:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/6.3.1/bits/stl_map.h:792:9: note: candidate: template<class _Pair, class> std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, _Pair&&) [with _Pair = _Pair; <template-parameter-2-2> = <template-parameter-1-2>; _Key = std::__cxx11::basic_string<char>; _Tp = Foo&; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >]
         insert(const_iterator __position, _Pair&& __x)
         ^~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:792:9: note:   template argument deduction/substitution failed:
main.cpp:12:48: note:   candidate expects 2 arguments, 1 provided
     myMap.insert(std::make_pair("hello", *this));
                                                ^
In file included from /usr/include/c++/6.3.1/map:61:0,
                 from main.cpp:2:
/usr/include/c++/6.3.1/bits/stl_map.h:807:9: note: candidate: template<class _InputIterator> void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = std::__cxx11::basic_string<char>; _Tp = Foo&; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Foo&> >]
         insert(_InputIterator __first, _InputIterator __last)
         ^~~~~~
/usr/include/c++/6.3.1/bits/stl_map.h:807:9: note:   template argument deduction/substitution failed:
main.cpp:12:48: note:   candidate expects 2 arguments, 1 provided
     myMap.insert(std::make_pair("hello", *this));

我不知道如何将指针 this 变成引用,然后我可以将其插入到地图中。我将如何修复代码?

【问题讨论】:

  • 我认为std::map&lt;std::string, Foo&amp;&gt; myMap; 应该是std::map&lt;std::string, Foo*&gt; myMap; & 只是将数据类型“转换”为地址

标签: c++ reference


【解决方案1】:

标准库容器不能包含引用。改用指针,或将您对T 的引用包装在std::reference_wrapper&lt;T&gt; 中,例如

#include <functional>
#include <map>
#include <string>

std::map<std::string, std::reference_wrapper<Foo> > myMap;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多