为什么模板功能会"no matching function for call to '…'"?
Why am I getting "no matching function for call to '…'" with template function?
使用以下代码:
材料h:
#ifndef MATERIA_H
#define MATERIA_H
class material
{
public:
template <class type>
static material* MakeMaterial(typename type::configtype, long);
template <class type>
void CreateNaturalForm(typename type::configtype, long);
…
};
template <class type>
material* material::MakeMaterial(typename type::configtype Config, long Volume)
{
return type::Spawn(Config, Volume);
}
#endif
材料:
#ifndef MATERIAS_H
#define MATERIAS_H
#include "materia.h"
#include "confdef.h"
class solid : public material {
public:
typedef solidmaterial configtype;
…
};
template material* material::MakeMaterial<solid>(solidmaterial, long);
template <class type>
void material::CreateNaturalForm(typename type::configtype Config, long Volume)
{
…
MakeMaterial(Config, Volume); // Error here
…
}
template void material::CreateNaturalForm<solid>(solidmaterial, long);
#endif
confdef.h:
#ifndef CONFDEF_H
#define CONFDEF_H
enum solidmaterial {
WOOD,
…
};
#endif
main.cpp
#include "materia.h"
#include "materias.h"
#include "confdef.h"
int main()
{
material::MakeMaterial(WOOD, 500); // Same error here
}
(这是上面代码的在线版本,再现了错误。)
我在评论行收到以下编译错误消息:
调用"MakeMaterial"没有匹配功能
我做错了什么?显式实例化难道不应该让编译器看到正确的函数吗?
如果我显式编写MakeMaterial<solid>
,代码就会编译,但这里的重点是从Config
参数推导出type
。我怎样才能做到这一点?
在调用时
MakeMaterial(Config, Volume); // Error here
编译器被要求找到其中函数模板中的type::configtype
是Config
的类型的匹配。
但是没有什么告诉编译器type
匹配到什么:这不是一个显式实例化。
一般来说,type
可以匹配数百种类型,其中type::configtype
将是Config
的类型。C++不支持只有一种可能类型的特殊情况。
如何解决这个问题取决于你想要完成什么。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 模板规范获取'Ambiguous call to overloaded function'
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 结构错误:"no matching function for call to "
- C++线程"Call to non-static member function without an object argument"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- "error: no matching function for call to",我无法更改结构的定义
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 哈纳简单折叠"call to non-constexpr function"
- "error: no matching function for call to ‘push_back(char [6])" .如何解决此代码中的错误?
- 使用 std::min "no matching function for call to ‘min(<brace-enclosed initializer list>)’"时出错
- 如何修复"error: call to 'abs' is ambiguous"
- "call to 'triangle' is ambiguous"错误
- 如何修复 C++ 中的"no matching fuction for call to:"错误
- 如何修复C++中的"no matching function to call to..."?
- 如何修复从基类继承两次时"error: no matching function for call to"
- 提升::访客操作员过载的变体"Error: no match for call to [...]"