5
votes

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.

2
Use std::map<X,Y>::value_type is std::pair<*const* X, Y>.JoeG
also functions["sin"] = 1; would work perfectly :-)Martin Kristiansen
@MartinKristiansen That's what I would do. It's slightly more expensive, and requires that the value is default constructable. That's fine for integers, but for some classes it might be prohibitive or impossible.Peter Wood
About your edit: You don't need to define temp before inserting, that is not the mistake, and also you don't need to call make_pair – although it has the advantage not needing to explicity specify the types –, the pair-constructor also works. Actually I would vote for @MartinKristiansen solution because it's short and readable.Christian Ammer

2 Answers

8
votes

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));
3
votes
  1. You have not included <string>
  2. char** argv[] has to be const char* argv[]