jeudi 1 septembre 2022

no matching function for call to ‘ ’ [duplicate]

Here's the definition of my class:

class F1Team {
    private:
        string name;
        string racer1;
        string racer2;

    protected:
        int titles;
    
    public:
        F1Team(string tnm) {
            name = tnm;
            titles = 0;
            
        }
        void set_racer1(string r1) {
            racer1 = r1;
        }
        void set_racer2(string r2) {
            racer2 = r2;
        }
        void display() {
            cout<< name<< endl;
        }
};

I want to store class objects mapping to an int (i.e., <int, [class]>)

Here's my code to implement it:

int main() {
    map<int, F1Team> mp;
    for (int i=0; i<3; ++i) {
        
        F1Team t {"Mercedes"};
        mp.insert(pair<int , F1Team>(i, t));
    }

    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        (*itr).second.display();
    }
    mp[1].display();
    return 0;
}

The code throws compilation error when I try to access the object by key.

Error:

In file included from /usr/include/c++/11/bits/stl_map.h:63,
                 from /usr/include/c++/11/map:61,
                 from test.cpp:3:
/usr/include/c++/11/tuple: In instantiation of ‘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 = F1Team]’:
/usr/include/c++/11/tuple:1813:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = F1Team]’
/usr/include/c++/11/ext/new_allocator.h:162:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, F1Team>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, F1Team> >]’
/usr/include/c++/11/bits/alloc_traits.h:516:17:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, F1Team>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, F1Team> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, F1Team> > >]’
/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, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, F1Team> >*]’
/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, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, F1Team> >*]’
/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, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree<int, std::pair<const int, F1Team>, std::_Select1st<std::pair<const int, F1Team> >, std::less<int>, std::allocator<std::pair<const int, F1Team> > >::iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree<int, std::pair<const int, F1Team>, std::_Select1st<std::pair<const int, F1Team> >, std::less<int>, std::allocator<std::pair<const int, F1Team> > >::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 = F1Team; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = F1Team; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
test.cpp:45:9:   required from here
/usr/include/c++/11/tuple:1824:9: error: no matching function for call to ‘F1Team::F1Team()’
 1824 |         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:17:9: note: candidate: ‘F1Team::F1Team(std::string)’
   17 |         F1Team(string tnm) {
      |         ^~~~~~
test.cpp:17:9: note:   candidate expects 1 argument, 0 provided
test.cpp:7:7: note: candidate: ‘F1Team::F1Team(const F1Team&)’
    7 | class F1Team {
      |       ^~~~~~
test.cpp:7:7: note:   candidate expects 1 argument, 0 provided
test.cpp:7:7: note: candidate: ‘F1Team::F1Team(F1Team&&)’
test.cpp:7:7: note:   candidate expects 1 argument, 0 provided

I don't understand why the constructor to my class is being called here.

Aucun commentaire:

Enregistrer un commentaire