调用自定义类的map的值函数

问题描述 投票:0回答:1

我有一个自定义类的 STL 映射,其中键是整数,值是我的类的对象。我想使用地图调用我的类的函数。我的代码如下:

#include <map>                                                                                                                                                                                                 
#include <iostream>

class myclass
{
        private:
                int x;
        public:
                myclass(int y)
                {
                        this->x = y;
                }

                int add_to_x(int y) const
                {
                        return (this->x + y); 
                }
};

int main()
{
        std::map<int, myclass> mymap;

        mymap.emplace(1, 10);
        mymap.emplace(2, 20);

        std::cout << mymap[1].add_to_x(100) << std::endl;
        std::cout << mymap[2].add_to_x(100) << std::endl;

        return 0;
}

但是,我使用

g++
版本
11.4.0
编译此代码,出现以下编译错误。我不确定我哪里错了。我该如何解决这个问题?

In file included from /usr/include/c++/11/bits/stl_map.h:63,
                 from /usr/include/c++/11/map:61,
                 from test.cpp:1:
/usr/include/c++/11/tuple: In instantiation of ‘constexpr std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {int&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = myclass]’:
/usr/include/c++/11/tuple:1809:63:   required from ‘constexpr std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = myclass]’
/usr/include/c++/11/bits/stl_construct.h:97:14:   required from ‘constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = std::pair<const int, myclass>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; decltype (::new(void*(0)) _Tp) = std::pair<const int, myclass>*]’
/usr/include/c++/11/bits/alloc_traits.h:518:4:   required from ‘static constexpr void std::allocator_traits<std::allocator<_Up> >::construct(std::allocator_traits<std::allocator<_Up> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, myclass>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, myclass> >; std::allocator_traits<std::allocator<_Up> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, myclass> > >]’
/usr/include/c++/11/bits/stl_tree.h:595:32:   required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, myclass>; _KeyOfValue = std::_Select1st<std::pair<const int, myclass> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, myclass> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, myclass> >*]’
/usr/include/c++/11/bits/stl_tree.h:612:21:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, myclass>; _KeyOfValue = std::_Select1st<std::pair<const int, myclass> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, myclass> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, myclass> >*]’
/usr/include/c++/11/bits/stl_tree.h:2431:33:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, myclass>; _KeyOfValue = std::_Select1st<std::pair<const int, myclass> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, myclass> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree<int, std::pair<const int, myclass>, std::_Select1st<std::pair<const int, myclass> >, std::less<int>, std::allocator<std::pair<const int, myclass> > >::iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree<int, std::pair<const int, myclass>, std::_Select1st<std::pair<const int, myclass> >, std::less<int>, std::allocator<std::pair<const int, myclass> > >::const_iterator]’
/usr/include/c++/11/bits/stl_map.h:520:37:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = myclass; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, myclass> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = myclass; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
test.cpp:27:29:   required from here
/usr/include/c++/11/tuple:1820:9: error: no matching function for call to ‘myclass::myclass()’
 1820 |         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:9:17: note: candidate: ‘myclass::myclass(int)’
    9 |                 myclass(int y)
      |                 ^~~~~~~
test.cpp:9:17: note:   candidate expects 1 argument, 0 provided
test.cpp:4:7: note: candidate: ‘constexpr myclass::myclass(const myclass&)’
    4 | class myclass
      |       ^~~~~~~
test.cpp:4:7: note:   candidate expects 1 argument, 0 provided
test.cpp:4:7: note: candidate: ‘constexpr myclass::myclass(myclass&&)’
test.cpp:4:7: note:   candidate expects 1 argument, 0 provided

据我了解,错误告诉我,当我使用

mymap
运算符访问
[]
时,它正在尝试创建
myclass
类的新对象。但是,我只想引用已插入地图的
myclass
对象。我怎样才能实现这个目标?

c++ compiler-errors stl g++
1个回答
0
投票

我知道该怎么做了。事实证明,如果值尚不存在,则

[]
运算符也会插入一个值。因此,它需要存在值类型的默认构造函数。因此,如果您只需要访问一个元素而不需要构造它,则可以使用
at(key)
。在我的示例中,将
mymap[1].add_to_x(100)
转换为
mymap.at(1).add_to_x(100)
解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.