在C ++中将对象的引用添加到地图

时间:2017-03-29 20:01:00

标签: c++ reference

我无法在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放入引用中,然后我可以将其插入到地图中。我将如何修复代码?

1 个答案:

答案 0 :(得分:2)

标准库容器不能包含引用。改为使用指针,或者将引用包含在T std::reference_wrapper<T>中,例如

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

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