Try   HackMD

問題描述

在我使用

 um[key] = MapNode(key, value);

把新的 MapNode 加到 unordered_map 時,出現

main.cc:17:10: note: candidate constructor not viable: requires 2 arguments, but 0 were provided

問題分析

第一時間以為 MapNode 的 constructor 沒有定義好。
但細看錯誤訊息時發現, compilation error 是在我把新的 MapNode 加到 unordered_map 時發生。

 um[key] = MapNode(key, value);

以下是完整的 code:

#include "unordered_map" #include "iostream" #include "string" using namespace std; class MapNode { private: string key_; string value_; public: explicit MapNode(string key, string value); string Show(); }; MapNode::MapNode(string key, string value): key_(key), value_(value){} string MapNode::Show(){ size_t size = key_.size() + value_.size() + 1; char buffer[size]; snprintf(buffer,size, "%s%s", key_.c_str(), value_.c_str()); return buffer; } int main(int argc, char *argv[]) { unordered_map<string, MapNode> um; string key = "hello"; string value = "world"; um[key] = MapNode(key, value); for (auto &n: um) { cout << n.second.Show()<< endl; } return 0; }

錯誤訊息:

$ g++ -std=c++11 -o main main.cc && ./main In file included from main.cc:1: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/unordered_map:437: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__hash_table:16: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/algorithm:653: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/functional:495: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__functional/bind_front.h:14: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__functional/perfect_forward.h:14: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1522:7: error: no matching constructor for initialization of 'MapNode' second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...) ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:252:11: note: in instantiation of function template specialization 'std::pair<const s td::string, MapNode>::pair<const std::string &, 0UL>' requested here : pair(__pc, __first_args, __second_args, ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/allocator.h:154:28: note: in instantiation of function template specialization 'std::pair<con st std::string, MapNode>::pair<const std::string &>' requested here ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...); ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/allocator_traits.h:290:13: note: in instantiation of function template specialization 'std::a llocator<std::__hash_node<std::__hash_value_type<std::string, MapNode>, void *>>::construct<std::pair<const std::string, MapNode>, const std::piecewise_construct_t &, std::t uple<const std::string &>, std::tuple<>>' requested here __a.construct(__p, _VSTD::forward<_Args>(__args)...); ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__hash_table:2473:20: note: in instantiation of function template specialization 'std::allocator_trait s<std::allocator<std::__hash_node<std::__hash_value_type<std::string, MapNode>, void *>>>::construct<std::pair<const std::string, MapNode>, const std::piecewise_construct_t &, std::tuple<const std::string &>, std::tuple<>, void>' requested here __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__hash_table:2093:29: note: in instantiation of function template specialization 'std::__hash_table<st d::__hash_value_type<std::string, MapNode>, std::__unordered_map_hasher<std::string, std::__hash_value_type<std::string, MapNode>, std::hash<std::string>, std::equal_to<std: :string>, true>, std::__unordered_map_equal<std::string, std::__hash_value_type<std::string, MapNode>, std::equal_to<std::string>, std::hash<std::string>, true>, std::alloca tor<std::__hash_value_type<std::string, MapNode>>>::__construct_node_hash<const std::piecewise_construct_t &, std::tuple<const std::string &>, std::tuple<>>' requested here __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...); ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/unordered_map:1743:21: note: in instantiation of function template specialization 'std::__hash_table<s td::__hash_value_type<std::string, MapNode>, std::__unordered_map_hasher<std::string, std::__hash_value_type<std::string, MapNode>, std::hash<std::string>, std::equal_to<std ::string>, true>, std::__unordered_map_equal<std::string, std::__hash_value_type<std::string, MapNode>, std::equal_to<std::string>, std::hash<std::string>, true>, std::alloc ator<std::__hash_value_type<std::string, MapNode>>>::__emplace_unique_key_args<std::string, const std::piecewise_construct_t &, std::tuple<const std::string &>, std::tuple<> >' requested here return __table_.__emplace_unique_key_args(__k, ^ main.cc:30:5: note: in instantiation of member function 'std::unordered_map<std::string, MapNode>::operator[]' requested here um[key] = MapNode(key, value); ^ main.cc:7:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided class MapNode { ^ main.cc:7:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided main.cc:17:10: note: candidate constructor not viable: requires 2 arguments, but 0 were provided MapNode::MapNode(string key, string value): key_(key), value_(value){} ^

待解問題: