C++ 错误:没有匹配的函数调用'make_pair(int&, Quest*)'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3559344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: no matching function for call to 'make_pair(int&, Quest*)'
提问by Krevan
I get this weird error in g++; it compiles fine in Visual Studio.
我在 g++ 中遇到了这个奇怪的错误;它在 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*)'
DataFilesStructure.cpp:9135:58: 错误:没有匹配的函数调用'make_pair(int&, Quest*)'
回答by Johannes Schaub - litb
You are most probably using the C++0x version of the libstdc++ library. C++0x declares make_pair
as
您很可能正在使用 libstdc++ 库的 C++0x 版本。C++0x 声明make_pair
为
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
如果T1
是int
,则x
是int&&
,因此不能取左值类型int
。很明显,make_pair
被设计为在没有显式模板参数的情况下被调用
make_pair(*id, NULL)
回答by fredoverflow
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?
另外,一个 9000 行的 cpp 文件,真的吗?
回答by LBF
Simply remove the template parameters:
只需删除模板参数:
Cache.insert(make_pair(*id, NULL));
This should fix your problem.
这应该可以解决您的问题。
回答by kuchaguangjie
If a NULL
value is need for the 2 value, maybe an explicit type conversion is needed:
如果NULL
2 值需要一个值,则可能需要显式类型转换:
return make_pair((node)NULL,(node)NULL); // NULL value
return make_pair((node *)NULL,(node *)NULL); // NULL pointer value
回答by lourencoj
This may be coming a bit late for you guys but could be useful for others.
这对你们来说可能有点晚,但对其他人可能有用。
Had exactly the same problem:
有完全相同的问题:
strVar= ...
newNode= ...
static map<string, Node*> nodes_str;
nodes_str.insert(make_pair(strVar, newNode)); // all OK
to
到
intVar= ...
newNode= ...
static map<int, Node*> nodes_int;
nodes_int.insert(make_pair(intVar, newNode)); // compile error
solved it by adding:
通过添加解决它:
using std::make_pair;
回答by Billy ONeal
NULL
is not a Quest*
-- it may be being defined as ((void *)0) somewhere, which is not implicitly convertible to Quest*
. Use static_cast<Quest*>(0)
instead.
NULL
不是Quest*
- 它可能在某处被定义为 ((void *)0) ,它不能隐式转换为Quest*
. 使用static_cast<Quest*>(0)
来代替。