Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::map.insert "could not deduce template argument for..."

Tags:

c++

map

stl

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.

like image 901
xst Avatar asked Jan 29 '12 21:01

xst


2 Answers

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));
like image 81
Sergey Vyacheslavovich Brunov Avatar answered Nov 06 '22 03:11

Sergey Vyacheslavovich Brunov


  1. You have not included <string>
  2. char** argv[] has to be const char* argv[]
like image 3
Christian Ammer Avatar answered Nov 06 '22 01:11

Christian Ammer