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.
Author by
Krevan
Updated on April 08, 2020Comments
-
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 over 13 yearsIt'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 over 13 years@Krevan: No, it is possible that it cannot be converted to one.
-
Krevan over 13 yearsIt's automatically generated, and yes, int() seems to fix it!!
-
fredoverflow over 13 years@Billy: Isn't
NULL
defined to be0
in C++? -
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 over 13 years@Billy: I see. Looking forward to C++0x's
nullptr
even more :) -
Kos over 13 yearsWhat actually has happened here? We have an explicit template function instance
make_pair<int,Quest*)(int, Quest*)
and it's called with arguments of typesint&
and eitherint
orvoid*
. Then:int&
is implicitly convertible toint
andNULL
may or may not be implicitly convertible toQuest*
, 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 over 13 years@Kos: I'm not certain, it probably has to do with the fact that
*id
is an lvalue whileint(*id)
is an rvalue, but this is more of a gut feeling :) -
Kos over 13 yearsOK, I needed to check, I don't buy it :). On my GCC 4.5.1:
make_pair<int, Class*>(*p, 0);
wherep
isint*
compiles w/o warnings, but does NOT compile when I supply avoid*
instead ofint
as the second parameter - irregardless if I passint
orint&
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 about 13 yearsI would really like the downvoter to explain him/herself so that I can fix the answer if it's broken.
-
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 over 7 yearsThis helped me. C++11 doesn't require the template params.
-
Cauchy Schwarz over 3 yearsFor 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.