我必须返回一个指向类的指针
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类创建联系人。我该如何解决这个编译问题?
答案 0 :(得分:1)
为了使用operator[]
中的std::map
,该值必须是默认可构造的。尝试在Contact
类中添加不带参数的公共构造函数。您也可以尝试避免使用运算符:
Contact* PhoneBook::SearchById(unsigned int id) {
auto c = contacts_.find(id);
if (c != contacts_.end()) {
return &c->second;
}
return nullptr;
}
答案 1 :(得分:0)
变量contacts_
是std::map
。
您可以传递地图项的副本。
您可以传递地图项的迭代器。
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();
}
答案 2 :(得分:0)
如前所述,地图运算符[]搜索项并在未找到项的情况下创建它。为此,需要默认的公共构造函数。如果您使用 contacts_.find 方法,则已经创建了类实例,并且不需要构造函数,因此效果很好。 但是如何填写联系人_ ?我想应该是公共默认构造函数的同一问题。解决方案可以是
friend struct std::pair<const unsigned int, Contact>;
直接在
之后friend class PhoneBook;
这允许访问 pair 的构造函数,该构造函数用于map中并负责实例创建。但是您可以再授予一个类的构造函数访问权限。
或者(例如,朋友只是PhoneBook),您可以使用指针或智能指针来保持联系:
std::unordered_map<unsigned int, std::unique_ptr<Contact> > contacts_;
否定的是,您需要手动创建 Contact 的实例,因为 contacts_ [ID] 只能创建需要填充的空指针。
请记住使用 friend 是您需要重新设计课程的症状。