std::map 插入,候选人需要 2 个参数,提供 1 个
std::map insert, candidate expects 2 arguments, 1 provided
这是我的 header:
class L {
public:
L(wstring);
~L();
private:
wstring ipath;
std::unique_ptr<freeling::tokenizer> tokenizer;
};
这是我的 class:
L::L(wstring language) {
}
L::~L() {
}
这是主要内容:
std::map<std::string, L> l;
l.insert(std::make_pair("a", L(L"b")));
当我编译时,我得到了一个巨大的错误列表,但最后是:
/usr/include/c++/6/bits/stl_map.h:807:9: note: template argument deduction/substitution failed:
main.cpp:18:42: note: candidate expects 2 arguments, 1 provided
l.insert(std::make_pair("a", L(L"b")));
这是整个错误:https://pastebin.com/iU9bsBVH
奇怪的是,如果我只是删除析构函数的定义和声明,代码会编译
您在此处发布的第一行代码有误
std::pair<iterator,bool> insert( const value_type& value );
你应该传递两个值来插入。
例如
std::pair <std::string,double> product1; // default constructor
std::pair <std::string,double> product2 ("tomatoes",2.30); // value init
std::pair <std::string,double> product3 (product2); // copy constructor
以下代码编译通过。
#include <map>
#include <string>
using std::string;
class LPro
{
public:
LPro(const wchar_t* _pr){ m_pr = const_cast<wchar_t*> (_pr); }
private:
wchar_t* m_pr;
};
int main()
{
std::map<string, LPro> l_pro;
l_pro.insert(std::make_pair("pt", LPro(L"pt")));
return 0;
}
您是否不小心忘记了 l_pro 中的 'l'?
从编译器评论看来是这样。
这是我的 header:
class L {
public:
L(wstring);
~L();
private:
wstring ipath;
std::unique_ptr<freeling::tokenizer> tokenizer;
};
这是我的 class:
L::L(wstring language) {
}
L::~L() {
}
这是主要内容:
std::map<std::string, L> l;
l.insert(std::make_pair("a", L(L"b")));
当我编译时,我得到了一个巨大的错误列表,但最后是:
/usr/include/c++/6/bits/stl_map.h:807:9: note: template argument deduction/substitution failed:
main.cpp:18:42: note: candidate expects 2 arguments, 1 provided
l.insert(std::make_pair("a", L(L"b")));
这是整个错误:https://pastebin.com/iU9bsBVH
奇怪的是,如果我只是删除析构函数的定义和声明,代码会编译
您在此处发布的第一行代码有误
std::pair<iterator,bool> insert( const value_type& value );
你应该传递两个值来插入。
例如
std::pair <std::string,double> product1; // default constructor
std::pair <std::string,double> product2 ("tomatoes",2.30); // value init
std::pair <std::string,double> product3 (product2); // copy constructor
以下代码编译通过。
#include <map>
#include <string>
using std::string;
class LPro
{
public:
LPro(const wchar_t* _pr){ m_pr = const_cast<wchar_t*> (_pr); }
private:
wchar_t* m_pr;
};
int main()
{
std::map<string, LPro> l_pro;
l_pro.insert(std::make_pair("pt", LPro(L"pt")));
return 0;
}
您是否不小心忘记了 l_pro 中的 'l'? 从编译器评论看来是这样。