std::map<std::string,int> Problem

Discussion in 'C++' started by tombert, Aug 24, 2012.

  1. tombert

    tombert Guest

    Hi all,

    can someone please tell me what's wrong with that code?
    I am using gcc 4.7 from MinGW, under 4.6 it seemed to have compiled correctly.

    The error is:
    : error: no matching function for call to 'std::map<std::basic_string<char>, int>::insert(std::string&, int)'

    Example:
    class TDir {
    typedef std::map<std::string,int> Subdirs;
    public:
    TDir(){}
    ~TDir(){}
    void getDir(std::string name) {
    Subdirs t;
    t.insert(name,0);
    }
    };
    tombert, Aug 24, 2012
    #1
    1. Advertising

  2. tombert

    Öö Tiib Guest

    On Friday, August 24, 2012 4:21:29 PM UTC+3, tombert wrote:
    > Hi all,
    >
    >
    >
    > can someone please tell me what's wrong with that code?
    >
    > I am using gcc 4.7 from MinGW, under 4.6 it seemed to have compiled correctly.
    >
    >
    >
    > The error is:
    >
    > : error: no matching function for call to 'std::map<std::basic_string<char>, int>::insert(std::string&, int)'
    >
    >
    >
    > Example:
    >
    > class TDir {
    >
    > typedef std::map<std::string,int> Subdirs;
    >
    > public:
    >
    > TDir(){}
    >
    > ~TDir(){}
    >
    > void getDir(std::string name) {
    >
    > Subdirs t;
    >
    > t.insert(name,0);


    Should be:

    t.insert( std::pair<std::string,int>(name,0) );
    >
    > }
    >
    > };
    Öö Tiib, Aug 24, 2012
    #2
    1. Advertising

  3. tombert

    Luca Risolia Guest

    On 24/08/2012 15:21, tombert wrote:
    > Hi all,
    >
    > can someone please tell me what's wrong with that code?
    > I am using gcc 4.7 from MinGW, under 4.6 it seemed to have compiled correctly.
    >
    > The error is:
    > : error: no matching function for call to 'std::map<std::basic_string<char>, int>::insert(std::string&, int)'


    > class TDir {
    > typedef std::map<std::string,int> Subdirs;
    > public:
    > TDir(){}
    > ~TDir(){}
    > void getDir(std::string name) {
    > Subdirs t;
    > t.insert(name,0);

    ^^^^^^

    It's strange gcc4.6 compiled that code.

    try with std::make_pair from <utility>:
    t.insert(std::make_pair(name,0));
    Luca Risolia, Aug 24, 2012
    #3
  4. Öö Tiib <> wrote:
    > t.insert( std::pair<std::string,int>(name,0) );


    How about just

    t.insert( std::make_pair(name,0) );

    Or even easier:

    t[name] = 0;
    Juha Nieminen, Aug 25, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,263
    Ivan Vecerina
    Mar 17, 2005
  2. =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

    std::map<int, std::string[2]> problems

    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 13, 2005, in forum: C++
    Replies:
    18
    Views:
    4,596
    Stephen Howe
    Jun 16, 2005
  3. Schnoffos
    Replies:
    2
    Views:
    1,195
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,609
    Old Wolf
    Jan 20, 2004
  5. Angus
    Replies:
    3
    Views:
    319
Loading...

Share This Page