【问题标题】:Invalid use of template-name 'SmartArray' without an argument list没有参数列表的模板名称“SmartArray”的使用无效
【发布时间】:2016-11-10 22:06:18
【问题描述】:

所以标题说明了我在使用类构造函数/析构函数时遇到的问题。这是我的代码:

    template <class ArrType>
    class SmartArray{
        public:
        ArrType *elements;              // pointer that will point to dynamic array
        int length();                   // function to return array length
        SmartArray<ArrType>();          // default constructor
        SmartArray(int arrSize);        // constructor that initializes array size
        ~SmartArray();                  // destructor
        void resizeArr(int newsize);        // function that resizes array
        SmartArray(const SmartArray& otherObject);  // copy constructor

这是默认构造函数:

    SmartArray::SmartArray(){
        arrSize = 0;
        elements = new ArrType[arrSize];
        cout << "Created array using default constructor." << endl;     // letting user know that object was successfully created
    }

我确实尝试过查找问题,但要么是那些程序太先进,我无法理解,要么我太愚蠢了。无论如何,我希望有一个简单的解决方法。

【问题讨论】:

  • SmartArray&lt;ArrType&gt;(); 应该只是 SmartArray();

标签: c++ class templates constructor


【解决方案1】:

您必须使用template 关键字来定义构造函数:

template<class ArrayType>
SmartArray<ArrayType>::SmartArray(){

   // ...

附:如果您可以确认您已将此构造函数定义放在单独的 .cpp 文件中,那么不幸的是,您的问题也必须以 a duplicate of this question 的形式关闭。

【讨论】:

    猜你喜欢
    • 2013-08-13
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多