c++ - 模板无法将函数定义与现有声明相匹配

标签 c++ class templates error-handling declaration

我正在处理 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/

相关文章:

c++ - C++中的动态内存问题

c++ - Winmouse在C++控制台上的坐标

c++ - 在 C++ 中对用户定义的类型使用 extern 关键字

jquery - Highcharts 回流不适用于类选择器

c++ - C++ 映射的运算符重载

c++ - 重载+添加两个指针

java - 检索类类型,并实例化一个相同类型的新类

vb.net - 使用 Reflection.PropertyInfo

c++ - 为什么模板类的成员需要通过其模板类的参数进行参数化

c++ - std::pair 作为模板<class> 参数? C++