【问题标题】:Trying to return class type but error with template parameters尝试返回类类型但模板参数出错
【发布时间】:2016-11-12 00:20:03
【问题描述】:

我总共有 5 个函数用于在一个类中重载 &、+、-、* 和 /,该类只有一个动态创建的数组,其中 arrSize 作为私有成员。我还使用模板来通用化类及其重载函数。在我遇到问题的 5 个函数中的每一个中,我都收到错误无效使用没有参数列表的模板名称“SmartArray”。这是我的功能:

/* function description: overrides the & operator. Lets us concatenate two arrays together.
   parameters:           const SmartArray& numArray is the right-hand array being concatenated to the left-hand array.
   return value:         void
*/
template <class ArrType>
SmartArray<ArrType>& SmartArray::operator&(const SmartArray& numArray) const{
    SmartArray concatenate(arrSize+numArray.length());   // creating object with array large enough to hold both arrays.
    for(int i=0; i<arrSize; i++)        // putting left-hand array into concatenated array
        concatenate.elements[i] = elements[i];
    for(int i=arrSize; i<arrSize+numArray.length(); i++)    // puts in right-hand array elements second
        concatenate.elements[i] = numArray.elements[i-arrSize];

    return concatenate;
}

/* function description: overrides + operator. Lets us add the contents of two arrays together.
   parameters:           const SmartArray& numArray is the right-hand array being added to the left-hand array.
   return value:         SmartArray
*/
template <class ArrType>
SmartArray SmartArray::operator+(const SmartArray& numArray) const{
    SmartArray added;    // initializes array to hold added arrays
    if(arrSize > numArray.length()){    // checks which array is larger, then creates new array with the larger size
        SmartArray added(arrSize);
        for(int i=0; i<numArray.length(); i++)
            added.elements[i] += numArray.elements[i];
        for(int i=numArray.length(); i<arrSize; i++)
            added.elements[i] = numArray.elements[i];
    }else if(arrSize <= numArray.length()){
        SmartArray added(numArray.length());
        for(int i=0; i<arrSize; i++)
            added.elements[i] += numArray.elements[i];
        for(int i=arrSize; i<numArray.length(); i++)
            added.elements[i] = numArray.elements[i];
    }
    return added;
}

/* function description: overrides + operator. Lets us subtract the contents of two arrays together.
   parameters:           const SmartArray& numArray is the right-hand array being subtracted to the left-hand array.
   return value:         SmartArray
*/
template <class ArrType>
SmartArray SmartArray::operator-(const SmartArray& numArray) const{
    SmartArray subtracted;    // initializes array to hold subtracted arrays
    if(arrSize > numArray.length()){    // checks which array is larger, then creates new array with the larger size
        SmartArray subtracted(arrSize);
        for(int i=0; i<numArray.length(); i++)
            subtracted.elements[i] -= numArray.elements[i];
        for(int i=numArray.length(); i<arrSize; i++)
            subtracted.elements[i] = numArray.elements[i];
    }else if(arrSize <= numArray.length()){
        SmartArray subtracted(numArray.length());
        for(int i=0; i<arrSize; i++)
            subtracted.elements[i] -= numArray.elements[i];
        for(int i=arrSize; i<numArray.length(); i++)
            subtracted.elements[i] = numArray.elements[i];
    }
    return subtracted;
}

/* function description: overrides + operator. Lets us multiply the contents of two arrays together.
   parameters:           const SmartArray& numArray is the right-hand array being multiplied to the left-hand array.
   return value:         SmartArray
*/
template <class ArrType>
SmartArray SmartArray::operator*(const SmartArray& numArray) const{
    SmartArray multiplied;    // initializes array to hold multiplied arrays
    if(arrSize > numArray.length()){    // checks which array is larger, then creates new array with the larger size
        SmartArray multiplied(arrSize);
        for(int i=0; i<numArray.length(); i++)
            multiplied.elements[i] *= numArray.elements[i];
        for(int i=numArray.length(); i<arrSize; i++)
            multiplied.elements[i] = numArray.elements[i];
    }else if(arrSize <= numArray.length()){
        SmartArray multiplied(numArray.length());
        for(int i=0; i<arrSize; i++)
            multiplied.elements[i] *= numArray.elements[i];
        for(int i=arrSize; i<numArray.length(); i++)
            multiplied.elements[i] = numArray.elements[i];
    }
    return multiplied;
}

/* function description: overrides + operator. Lets us divide the contents of two arrays together.
   parameters:           const SmartArray& numArray is the right-hand array being divided to the left-hand array.
   return value:         SmartArray
*/
template <class ArrType>
SmartArray SmartArray::operator/(const SmartArray& numArray) const{
    SmartArray divided;    // initializes array to hold divided arrays
    if(arrSize > numArray.length()){    // checks which array is larger, then creates new array with the larger size
       SmartArray divided(arrSize);
        for(int i=0; i<numArray.length(); i++)
            divided.elements[i] /= numArray.elements[i];
        for(int i=numArray.length(); i<arrSize; i++)
            divided.elements[i] = numArray.elements[i];
    }else if(arrSize <= numArray.length()){
        SmartArray divided(numArray.length());
        for(int i=0; i<arrSize; i++)
            divided.elements[i] /= numArray.elements[i];
        for(int i=arrSize; i<numArray.length(); i++)
            divided.elements[i] = numArray.elements[i];
    }
    return divided;
}

使用第一个功能,我尝试使用

SmartArray<ArrType>& SmartArray::.....

但我收到错误“'template class SmartArray' used without template parameters”。

【问题讨论】:

    标签: c++ class templates operator-overloading


    【解决方案1】:

    在任何使用SmartArray 类型的地方,都需要包含要使用的模板化类型。例如,

    template <class ArrType>
    SmartArray<ArrType> SmartArray<ArrType>::operator&(const SmartArray<ArrType>& numArray) const{
        SmartArray<ArrType> concatenate(arrSize+numArray.length());
    

    注意&lt;ArrType&gt;添加到参数列表中numArray的声明和concatenate局部变量。我还从函数返回类型中删除了&amp;,因为您不想返回对局部变量的引用。

    您必须对其他功能进行类似的更改。

    【讨论】:

    • 成功了!不过,为了让您知道,我还需要在每个双冒号 (::) 之前添加“”。
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多