无法让我的模板工作.非法使用显式模板参数

Cant get my template to work. Illegal use of explicit template arguments?

本文关键字:参数 工作 我的 非法      更新时间:2023-10-16

我试图使用模板作为变量类型通过类来创建我的不同单位。它说这是"非法使用显式模板参数"。

template <class Type>
void Build<Type>()
{
    pUnit = new Type();
    unitArray.push_back(pUnit);
}

我是否必须以某种方式指定类类型是单位?当我将"模板<类类型>"更改为"模板<单元类型>"时,它告诉我它是"非类型模板参数的非法类型"。我不知道我需要做什么才能使它合法化。

"

非法使用显式模板参数"只是无效的函数模板声明语法。<Type>在函数模板名称之后做什么?它应该是公正的

template <class Type>
void Build()
{
  // ...
}

我无法判断 unitArray 是什么,所以我假设它是 Type 的向量。我也分不清 pUnit 是什么,我假设它是类型类型。

你有一个名为 Build 的函数,将其替换为 void Build(){}。

除非这个函数在类实现中,否则你需要将 pUnit 和 unitArray 声明为我上面提到的类型的参数。

template <class Type>
void Build()
{
  pUnit = new Type;
  unitArray.push_back(pUnit);
}

template <class Type>
void Build(vector<Type> unitArray, Type * pUnit)
{
  pUnit = new Type;
  unitArray.push_back(pUnit);
}