gpt4 book ai didi

c++ - 为什么我用模板函数得到 "no matching function for call to ' …'"?

转载 作者:行者123 更新时间:2023-11-28 02:24:47 25 4
gpt4 key购买 nike

使用以下代码:

Material .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

Material .h:

#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
}

(Here 是重现错误的上述代码的在线版本。)

我在注释行中收到以下编译错误消息:

No matching function for call to 'MakeMaterial'

我做错了什么?显式实例化不应该让编译器看到正确的函数吗?

如果我写 MakeMaterial<solid> 代码编译明确地,但这里的重点是推断type来自 Config争论。我怎样才能做到这一点?

最佳答案

通话中

MakeMaterial(Config, Volume); // Error here

要求编译器找到一个匹配项,其中函数模板中的 type::configtypeConfig 的类型。

但是没有告诉编译器要将 type 匹配到什么:这不是显式实例化。

一般来说,type 可以匹配数百种类型,其中 type::configtypeConfig 的类型. C++ 不支持只有一种可能类型的特殊情况。

如何解决这个问题取决于您想要完成什么。

关于c++ - 为什么我用模板函数得到 "no matching function for call to ' …'"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com