【问题标题】:C++ constructor overloading error unable to match function definitionC++ 构造函数重载错误无法匹配函数定义
【发布时间】:2017-04-21 01:20:22
【问题描述】:

我正在使用 Microsoft Visual Studios,并且我创建了一个通用类 List_Array。默认构造函数没有问题,但是其他两个(重载的)构造函数正在生成错误。

//List_Array.h

template<typename T>
class List_Array {
private:
     int size; ...
     T* data;
public:
     List_Array<T>::List_Array();
     List_Array<T>::List_Array(int);
     List_Array<T>::List_Array(const T&, int);
     ...
};

template<typename T>
List_Array<T>::List_Array() { }

template<typename T>
List_Array<T>::List_Array(int s) {
     this->size = s
     this->data = new T[s];
}

template<typename T>
List_Array<T>::List_Array(const T& init, int s){
     this->size = s;
     this->data = new T[s];
     for (int i = 0; i < s; i++){
          this->data[i] = init;
     }
}

我得到一个 C2244 'List_Array::List_Array': 无法将函数定义与现有声明匹配

非常感谢任何帮助!

【问题讨论】:

    标签: c++ constructor compiler-errors overloading


    【解决方案1】:

    问题与模板或重载无关。您只是不需要List_Array&lt;T&gt;:: 部分用于类定义中的成员函数声明。即

    template<typename T>
    class List_Array {
    private:
         int size; ...
         T* data;
    public:
         List_Array();
         List_Array(int);
         List_Array(const T&, int);
         ...
    };
    

    LIVE

    【讨论】:

    • 谢谢!我没有意识到这会导致问题。
    • @TylerHughes 请注意,非模板类也是如此。所以这里的问题与模板和重载无关。
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2019-04-14
    • 1970-01-01
    • 2020-06-09
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多