在我使用
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){}
^
待解問題:
SQL Query
Sep 28, 2023如題,為了實作一個 LRU_Cache,我使用 unordered_map um 來記錄新加入 list 尾端元素的 iterator,但是我發現 um 內存放的 iterator 指向的數值卻會隨著其他元素被 push_back 時改變,有辦法避免其他情況嗎?
Jun 21, 2023Host: Eric Chang tags: AWS, ElastiCache, AuroraDB 目標: 每個 Part 需要做一些基本介紹,然後給出一個 Demo,讓大家了解對應服務使用上的細節 Part1: RDS 成員: AC, Gin For relation database: Postgres, MySQL, MariaDB, Oracle, Microsoft SQL Server, Aurora
Mar 20, 2023tags: AWS, EC2, EBS, AMI Rick: 聊聊 EBS (Elastic Block Store) 的特性,使用情境 即使 EC2 被關閉了,EBS 可以被保留下來,供以後使用 Volume Typegp2 / gp3 General Purpose (can use as Root Volume) io1 / io2 High Performance (can use as Root Volume) st1 low cost sc1 lowest cost Mount to 1 Instance only (Multi-attach io1 & io2 only)
Mar 14, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up