我正在处理 Array 的模板类,但似乎该函数与声明不匹配,我不明白为什么:
template <class Type>
class Array
{
private:
Type* m_data;
unsigned int _size;
public:
template <class Type> Array(unsigned int initial_size); // Function
}
由于函数本身与声明匹配,我什至按了 Ctrl+'.'让 VS 执行该功能但仍然出现错误。我看到了类似的问题,但似乎他们都在我没有的参数中使用了 Type。
template<class Type>
Array<Type>::Array(unsigned int initial_size)
{
_size = initial_size;
m_data = new Type(initial_size);
}
'Array::Array': unable to match function definition to an existing declaration
为什么会出现错误?
最佳答案
无需在构造函数声明中使用template
:
template <class Type> Array(unsigned int initial_size);
只需使用:
Array(unsigned int initial_size);
关于c++ - 模板无法将函数定义与现有声明相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157968/