2016-11-12 5 views
0

&、+、 - 、*、/は、プライベートメンバーとしてarrSizeを持つ動的に作成された配列しか持たないクラスに合計5つの関数があります。また、クラスとそのオーバーロードされた関数を共通化するためのテンプレートも使用しています。私が問題を抱えている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::..... 

を使用してみましたが、私はエラーだ「テンプレートパラメータなしで使用 『テンプレートクラスSmartアレイ』を。」

答えて

0

SmartArrayタイプを使用する場合は、使用するテンプレートタイプを含める必要があります。例えば、

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

注パラメータリスト内numArrayの宣言とconcatenateローカル変数に<ArrType>の添加。また、ローカル変数への参照を返さないため、戻り値の型から&を削除しました。

他の機能にも同様の変更を加える必要があります。

+0

それがうまくいった!しかし、あなたに知らせるために、私はまた、すべての二重コロン(::)の前に ""を追加する必要がありました。 –

関連する問題