2016-11-12 102 views
0

我總共有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::..... 

,但我得到了錯誤「‘模板類的SmartArray’不使用模板參數。」

回答

0

無處不在您使用SmartArray類型,您需要包含要使用的模板類型。例如,

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

注意添加<ArrType>numArray在參數列表中的聲明和concatenate局部變量。我也從函數返回類型中刪除了&,因爲你不想返回一個對局部變量的引用。

您必須對其他功能進行類似的更改。

+0

這工作!但是,爲了讓你知道,我還需要在每個雙冒號(::)前添加「」。 –