error: no matching function for call to 'make_pair(int&, Quest*)'

26,952

Solution 1

Does it work with an explicit cast?

if (id)
    Cache.insert(make_pair<int, Quest*>(int(*id), NULL));

Also, a cpp file with 9000 lines, really?

Solution 2

You are most probably using the C++0x version of the libstdc++ library. C++0x declares make_pair as

template <class T1, class T2>
pair<V1, V2> make_pair(T1&& x, T2&& y) noexcept;

If T1 is int, then x is int&&, and therefor cannot take lvalues of type int. Quite obviously, make_pair is designed to be called without explicit template arguments

make_pair(*id, NULL)

Solution 3

Simply remove the template parameters:

Cache.insert(make_pair(*id, NULL));

This should fix your problem.

Share:
26,952
Krevan
Author by

Krevan

Updated on April 08, 2020

Comments

  • Krevan
    Krevan about 4 years

    I get this weird error in g++; it compiles fine in Visual Studio.

    struct Quest
    {
        static map<int, Quest*> Cache;
    };
    
    Quest *Quest::LoadFromDb(BaseResult& result, int *id)
    {
        Quest *ret;
        if(result.Error())
        {
            if(id)
                Cache.insert(make_pair<int, Quest*>(*id, NULL)); // <--- Problematic line
    
            return NULL;
        }
    
    // ...
    }
    

    Exact error:

    DataFilesStructure.cpp:9135:58: error: no matching function for call to 'make_pair(int&, Quest*)'

  • Krevan
    Krevan over 13 years
    It's not but it can be converted to one, Quest *a = NULL for example.. And I explicitly list the template parameters. Anyways I tried, same error.
  • Billy ONeal
    Billy ONeal over 13 years
    @Krevan: No, it is possible that it cannot be converted to one.
  • Krevan
    Krevan over 13 years
    It's automatically generated, and yes, int() seems to fix it!!
  • fredoverflow
    fredoverflow over 13 years
    @Billy: Isn't NULL defined to be 0 in C++?
  • Billy ONeal
    Billy ONeal over 13 years
    @FredOverflow: Normally yes, but it's possible for it to be redefined by a C library expecting ((void *)0).
  • fredoverflow
    fredoverflow over 13 years
    @Billy: I see. Looking forward to C++0x's nullptr even more :)
  • Kos
    Kos over 13 years
    What actually has happened here? We have an explicit template function instance make_pair<int,Quest*)(int, Quest*) and it's called with arguments of types int& and either int or void*. Then: int& is implicitly convertible to int and NULL may or may not be implicitly convertible to Quest*, depending on the definition (don't use NULL in C++, NULL is in C!). Anyway, my question is: How COULD an explicit cast of FIRST argument fix the problem?
  • fredoverflow
    fredoverflow over 13 years
    @Kos: I'm not certain, it probably has to do with the fact that *id is an lvalue while int(*id) is an rvalue, but this is more of a gut feeling :)
  • Kos
    Kos over 13 years
    OK, I needed to check, I don't buy it :). On my GCC 4.5.1: make_pair<int, Class*>(*p, 0); where p is int* compiles w/o warnings, but does NOT compile when I supply a void* instead of int as the second parameter - irregardless if I pass int or int& as the first - as expected. How come this fixed the matter for the OP on GCC? @Krevan, admit it :) - you changed NULL to 0 or you changed the includes so that your NULL got redefined by another header to 0... it HAS to be something else! I doubt that we've just discovered a GCC bug...
  • Billy ONeal
    Billy ONeal about 13 years
    I would really like the downvoter to explain him/herself so that I can fix the answer if it's broken.
  • PHcoDer
    PHcoDer over 7 years
    @Krevan If I have an integer variable i. I want to make pair with i and another object. How exactly should I call makepair. 1) make_pair<*i, obj> 2) int&& j = i; make_pair<j, obj>? Both are not working. Whats the correct way to do it?
  • sgowd
    sgowd over 7 years
    This helped me. C++11 doesn't require the template params.
  • Cauchy Schwarz
    Cauchy Schwarz over 3 years
    For make_pair in C++11 when you specify V1, V2 explicitly, there is no type deduction and arguments x and y are required to be rvalue. But if you don't specify V1, V2, type deduction happens, x and y do not need to be rvalue.