std::map.insert“无法推断出...的模板参数”
我正在尝试熟悉 STL 库,但无法理解我的编译错误。我使用编译器错误字符串“无法推断模板参数...”搜索了其他问题,但没有一个答案似乎适用或相关。
错误 4 错误 C2784:'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)':无法推导模板参数对于“常量” std::unique_ptr<_Ty,_Dx>; &'来自 'const std::string' c:\program files (x86)\microsoft Visual Studio 10.0\vc\include\xfunction 125
我正在编写一个简单的解释器,用于计算一个变量中的导数/积分。我想要一个将用户输入与内部控制代码相匹配的映射。 key是一个trig(或其他)函数,int是控制代码。我使用一个单独的头文件来#define 函数,但对于本示例,我仅使用整数文字。我正在使用 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;
}
编辑:
在尝试 Serge 的(有效)答案之后:
functions.insert(std::make_pair(std::string("sin"), 1));
我意识到错误并尝试了这个:
pair<string, int> temp = pair<string,int>("cos",2);
functions.insert(temp);
虽然这可能不是最理想的,但它说明了在插入地图之前没有构造对对象的问题。
I'm trying to familiarize myself with the STL library, but I'm having trouble understanding my compilation error. 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
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. I'm using a separate header file to #define the functions, but for this example i'm just using integer literals. I'm using 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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

确保包含 string 标头。
Make sure that you have string header included.