获得海湾合作委员会"no matching function"
Got "no matching function" from GCC
为什么这个简单的代码不起作用?
template<class U>
class retype
{
typedef U type;
};
class object
{
public:
template<class U>
int create(typename retype<U>::type p)
{
return 4;
}
};
int main()
{
int n = object().create(5);
return 0;
}
使用GCC编译时出现这个错误:
test.cpp: In function ‘int main()’:
test.cpp:20: error: no matching function for call to ‘object::create(int)’
问题在哪里?
您依赖于从函数参数中推导模板参数。但是函数模板实参不能推导,因为它是一个不可推导的上下文。
更具体地说,即使retype<U>::type
是int
,模板参数U
也不能推导出来。因为retype
的专门化可能定义为:
template<>
struct retype<X>
{
typedef int type;
};
所以你看,给定retype<U>::type
是int
,模板参数U
也可以是X
。
实际上,可能有不止一个这样的专门化,它们都可以将type
定义为int
。所以没有一对一的关系。编译器不能唯一地推导出U
。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- C++ "error: no matching function"
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 为什么"std::isdigit"在 Windows 上报告C++"no matching overloaded function"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 替换派生对象向量中的对象"no matching function to call"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 为什么即使我定义了它们"no matching overload found"也会出现错误
- 为什么有一个指向此代码行(第 7 行)的"no matching call for function"错误?