Return class 的指针

Return pointer of class

我必须 return 指向函数 class 的指针

Contact* PhoneBook::SearchById(unsigned int id) {
    if (contacts_.find(id) != contacts_.end()) {
        return &(contacts_[id]);
    }
    return nullptr;
}

这里有 classes 定义:

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&&)’

根据这个我必须再做一个构造函数,所以我做了。但是编译器说它必须是 public,我不想让 Contact 构造函数 public。我希望联系人只能由 PhoneBook class 创建。我该如何解决这个编译问题?

变量contacts_是一个std::map
您可以传递地图项的副本。
您可以传递 个迭代器 个地图项。

operator[]std::map 的作用与对数组的作用不同。 operator[] 方法是一种搜索功能。参见 std::map

因此,std::map 的表达式 &contacts[i] 与数组的功能不同。

我的建议是更改函数的签名:

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();
}

为了使用 operator[] of std::map,该值必须是默认可构造的。尝试向 Contact class 添加一个不带参数的 public 构造函数。您也可以尝试避免使用运算符:

Contact* PhoneBook::SearchById(unsigned int id) {
    auto c = contacts_.find(id);
    if (c != contacts_.end()) {
        return &c->second;
    }
    return nullptr;
}

如前所述,地图运算符 [] 搜索项目并在找不到项目时创建它。为此需要默认的 public 构造函数。如果您使用 contacts_.find 方法,则 class 实例已经创建并且不需要构造函数,因此它运行良好。 但是contacts_怎么填呢?我想这应该是 public 默认构造函数的同一个问题。解决方案可以是

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

直接在

之后
friend class PhoneBook;

这允许访问 pair 的构造函数,它在 map 中使用并负责创建实例。但是您再授予对构造函数的访问权限 class...

或者(即好友只是电话簿)您可以使用指针或智能指针来保持联系人:

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

缺点是您需要手动创建 Contact 的实例,因为 contacts_[ID] 只能创建需要的空指针已满

请记住 friend 的用法是您需要重新设计 classes 的症状。