简体   繁体   English

std :: map.insert“无法推断出...的模板参数”

[英]std::map.insert “could not deduce template argument for…”

I'm trying to familiarize myself with the STL library, but I'm having trouble understanding my compilation error. 我正在尝试熟悉STL库,但是我无法理解编译错误。 I've searched for other questions using the compiler error string "could not deduce template argument for..." but none of the answers appear applicable or relevant. 我使用编译器错误字符串“无法推断出...的模板参数”搜索了其他问题,但没有一个答案显示适用或相关。

Error 4 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string' c:\\program files (x86)\\microsoft visual studio 10.0\\vc\\include\\xfunctional 125 错误4错误C2784:'bool std :: operator <(const std :: unique_ptr <_Ty,_Dx>&,const std :: unique_ptr <_Ty2,_Dx2>&)':无法推断'const std ::的模板参数unique_ptr <_Ty,_Dx>&'from'const std :: string'c:\\ program files(x86)\\ microsoft visual studio 10.0 \\ vc \\ include \\ xfunctional 125

I'm writing a simple interpreter for calculating derivatives/integrals in one variable. 我正在编写一个简单的解释器来计算一个变量中的导数/积分。 I want a map for matching the user's input to an internal control code. 我想要一个地图,用于将用户的输入与内部控制代码相匹配。 The key is a trig (or other) function, and the int is the control code. 键是trig(或其他)函数,int是控制代码。 I'm using a separate header file to #define the functions, but for this example i'm just using integer literals. 我正在使用单独的头文件来#define函数,但是对于这个例子,我只是使用整数文字。 I'm using Visual Studio: 我正在使用Visual Studio:

#include <cstdio>
#include <map>
using namespace std;
int main(int argc, char** argv) {
    map< string, int > functions;
    functions.insert( pair<string, int>("sin", 1) );

    return 0;
}

EDIT: 编辑:

after trying Serge's (which worked) answer: 在尝试了Serge(有效)后回答:

functions.insert(std::make_pair(std::string("sin"), 1));

i realized the mistake and tried this: 我意识到了这个错误并尝试了这个:

pair<string, int> temp = pair<string,int>("cos",2);
functions.insert(temp);

though this is probably suboptimal, it illustrates the issue of not having constructed the pair object before inserting into the map. 虽然这可能不是最理想的,但它说明了在插入地图之前没有构造配对对象的问题。

Make sure that you have string header included. 确保包含字符串标题。

#include <map>
#include <utility>
#include <string>

...

std::map<std::string, int> functions;
functions.insert(std::make_pair(std::string("sin"), 1));
  1. You have not included <string> 您还没有包含<string>
  2. char** argv[] has to be const char* argv[] char** argv[]必须是const char* argv[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM