Возвращаемый указатель класса - PullRequest
0 голосов
/ 29 сентября 2018

Я должен вернуть указатель на класс в функции

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.Как я могу решить эту проблему компиляции?

Ответы [ 3 ]

0 голосов
/ 29 сентября 2018

Чтобы использовать operator[] из std::map, значение должно быть конструируемым по умолчанию.Попробуйте добавить открытый конструктор без аргументов в ваш класс Contact.Вы также можете попытаться избежать использования оператора:

Contact* PhoneBook::SearchById(unsigned int id) {
    auto c = contacts_.find(id);
    if (c != contacts_.end()) {
        return &c->second;
    }
    return nullptr;
}
0 голосов
/ 29 сентября 2018

Как уже отмечалось, оператор карты [] ищет элемент и создает его, если элемент не найден.Для этого необходим общедоступный конструктор по умолчанию.Если вы используете contacts_.find подход, экземпляры классов уже созданы, и конструктор не требуется, поэтому он работает хорошо.Но как заполнить contacts_ ?Я полагаю, что это должна быть та же проблема общедоступного конструктора по умолчанию.Решение может быть

friend struct std::pair<const unsigned int, Contact>;

сразу после

friend class PhoneBook;

. Это позволяет получить доступ к конструктору для пары , которая используется в карте и отвечает за создание экземпляра.Но вы предоставляете доступ к конструктору для еще одного класса ...

В качестве альтернативы (т. Е. Другом является только телефонная книга), вы можете использовать указатель или умный указатель для сохранения контактов:

std::unordered_map<unsigned int, std::unique_ptr<Contact> > contacts_;

ОтрицательныйВам нужно создать экземпляры Contact вручную, поскольку contacts_ [ID] может создавать только пустой указатель, который необходимо заполнить.

Пожалуйста, помните использование Друг - это признак того, что вам нужно изменить классы.

0 голосов
/ 29 сентября 2018

Переменная contacts_ представляет собой std::map.
Вы можете передавать копии элементов карты.
Вы можете передавать итераторов элементов карты.

The operator[] не работает с std::map так же, как с массивом.Метод operator[] является функцией поиска.См. std::map.

Итак, выражение &contacts[i] для std::map не функционирует так же, как массив.

Я предлагаю изменить сигнатуру функции:

bool Phonebook::SearchById(int id, Contact& c)
{
  const std::map<unsigned int, Contact>::const_iterator iter = contacts_.find(id);
  if (iter != contacts_.end())
  {
     c = iter->second;
  }
  return iter != contacts_.end();
}
...