Я должен вернуть указатель на класс в функции
Contact* PhoneBook::SearchById(unsigned int id) {
if (contacts_.find(id) != contacts_.end()) {
return &(contacts_[id]);
}
return nullptr;
}
Вот определения классов:
struct Contact {
public:
friend class PhoneBook;
private:
Number number_;
Name name_;
Address address_;
Contact(string number, string name, const string* address);
string GetUnifiedNumber() const;
std::vector<string> GetUnifiedName() const;
};
class PhoneBook {
public:
unsigned int AddContact(string number, string name, const string* address = nullptr);
Contact* SearchById(unsigned int id);
PhoneBook();
private:
std::unordered_map<unsigned int, Contact> contacts_;
};
Я получаю ошибку при компиляции
In file included from /usr/include/c++/8/bits/hashtable_policy.h:34,
from /usr/include/c++/8/bits/hashtable.h:35,
from /usr/include/c++/8/unordered_map:46,
from phone_book.cpp:4:
/usr/include/c++/8/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 = {const unsigned int&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const unsigned int; _T2 = Contact]’:
/usr/include/c++/8/tuple:1657:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const unsigned int&}; _Args2 = {}; _T1 = const unsigned int; _T2 = Contact]’
/usr/include/c++/8/ext/new_allocator.h:136:4: required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const unsigned int, Contact>; _Args = {const std::piecewise_construct_t&, std::tuple<const unsigned int&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const unsigned int, Contact>, false>]’
/usr/include/c++/8/bits/alloc_traits.h:475:4: 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 unsigned int, Contact>; _Args = {const std::piecewise_construct_t&, std::tuple<const unsigned int&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const unsigned int, Contact>, false>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::__detail::_Hash_node<std::pair<const unsigned int, Contact>, false> >]’
/usr/include/c++/8/bits/hashtable_policy.h:2082:36: required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const unsigned int&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const unsigned int, Contact>, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const unsigned int, Contact>, false>]’
/usr/include/c++/8/bits/hashtable_policy.h:711:8: required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](const key_type&) [with _Key = unsigned int; _Pair = std::pair<const unsigned int, Contact>; _Alloc = std::allocator<std::pair<const unsigned int, Contact> >; _Equal = std::equal_to<unsigned int>; _H1 = std::hash<unsigned int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = Contact; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = unsigned int]’
/usr/include/c++/8/bits/unordered_map.h:977:20: required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type&) [with _Key = unsigned int; _Tp = Contact; _Hash = std::hash<unsigned int>; _Pred = std::equal_to<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, Contact> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = Contact; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = unsigned int]’
phone_book.cpp:109:39: required from here
/usr/include/c++/8/tuple:1668:70: error: no matching function for call to ‘Contact::Contact()’
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
^
phone_book.cpp:113:1: note: candidate: ‘Contact::Contact(std::__cxx11::string, std::__cxx11::string, const string*)’
Contact::Contact(string number, string name, const string* address) {
^~~~~~~
phone_book.cpp:113:1: note: candidate expects 3 arguments, 0 provided
phone_book.cpp:33:8: note: candidate: ‘Contact::Contact(const Contact&)’
struct Contact {
^~~~~~~
phone_book.cpp:33:8: note: candidate expects 1 argument, 0 provided
phone_book.cpp:33:8: note: candidate: ‘Contact::Contact(Contact&&)’
В соответствии с этим я должен сделать еще один конструктор, так я и сделал.Но компилятор говорит, что он должен быть публичным, и я не хочу делать конструктор Contact публичным.Я хочу, чтобы Контакты могли быть созданы только классом PhoneBook.Как я могу решить эту проблему компиляции?