2012-05-13 22 views
1

演算子のオーバーロードに関する問題があります。私はどこにでも見えましたが、このエラーのための適切な解決策を見つけることができませんでした。ここに私のコードの一部です:エラーt 2エラーC2679:バイナリ '/':タイプの右オペランドを取る演算子が見つかりません(または許容される変換はありません)

Matrix<type> Matrix<type>::operator/(const Matrix& denom){ 

if(num_of_rows != denom.num_of_rows || num_of_cols != denom.num_of_cols) 
    throw string("Unable to divide (Different size)."); 
if(denom.contains(0)) 
    throw string("Unable to divide (Divide by zero)."); 

for(int i = 0; i < num_of_rows; i++) 
    for(int j = 0; j < num_of_cols; j++) 
     values[i][j] /= denom.values[i][j]; 
        // I KNOW THIS IS NOT HOW TO DIVIDE TWO MATRICES 

return *this; 
} 

void Matrix<type>::operator=(const Matrix& m) const { 

delete [][] values; 
num_of_rows = m.num_of_rows; 
num_of_cols = m.num_of_cols; 
values = new type*[num_of_rows]; 

for(int i = 0; i < num_of_rows; i++){ 
    *(values + i) = new type[num_of_cols]; 
    for(int j = 0; j < num_of_cols; j++) 
     values[i][j] = m.values[i][j]; 
} 
} 

そして、これは、Matrixクラスで、コンストラクタは2つの引数取ります

class Matrix{ 

private: 
    type** values; 
    int num_of_rows, num_of_cols; 

public: 
    Matrix(){} 
    Matrix(int, int); 
    type getElement(int, int); 
    void print(); 
    bool contains(type); 
    Matrix<type> operator/(const Matrix&); 
    void operator=(const Matrix&) const; 
}; 

template <class type> 

Matrix<type>::Matrix(int rows, int cols){ 

values = new type*[rows]; 
num_of_rows = rows; 
num_of_cols = cols; 

for(int i = 0; i < rows; i++){ 
    *(values + i) = new type[cols]; 
    for(int j = 0; j < cols; j++){ 
      type random = (type)rand()/3276.71; 
     values[i][j] = random; 
    } 
} 
} 

を、メインのコードのこの作品は、このエラーを与える:

srand(time(NULL)); 
Matrix<int> m1(3,5); // creating some objects 
Matrix<double> m2(3,5); // matrices’ elements are assigned randomly from 0 to 10 
Matrix<double> m3(5,5); 
Matrix<double> m4(5,6); 
if(!(m2.contains(0))){ 
    Matrix<double> m8(3,5); 
    m8=m1/m2; // THIS LINE GIVES ERROR 
    m8.print(); 
} 
+0

手元にあるものとは関係ありませんが、あなたの 'operator ='は自己割り当てにとって安全ではありません。一時的な配列に値をコピーし、最後に古いバッファを削除する前に 'values'を使ってその配列へのポインタを入れ替えるのが普通です。 –

答えて

4

m1はタイプMatrix<int>であるため、operator/の適切なオーバーロードを検索すると、

Matrix<int> Matrix<int>::operator/(const Matrix& denom); 

ここでパラメータタイプMatrixは、いわゆる注入されたクラス名を使用することに注意してください。これはがMatrix<int>であることを意味します。これは問題の(テンプレート)クラスです。ただし、m2の場合、operator/を呼び出す引数は、Matrix<double>です。 Matrix<double>からMatrix<int>への変換が適切でないため、コールは無効です。

可能性修正プログラムは、テンプレートであることをoperator/を変更することです:

// Declared inside Matrix<type>: 
template<typename Other> 
Matrix& operator/=(Matrix<Other> const& other); 

(私も良く、それは実際にやっているものを反映するオペレータを固定する自由を取った。)

ただし」次に、タイプMatrix<double>m8に割り当てるMatrix<int>operator/への呼び出しの結果)を使用しているという問題に直面します。変換を行うには、おそらくが必要です(変換コンストラクタもお勧めします)。または変換コンストラクタをコンバートせずにoperator=を使用することをお勧めします。

+0

ありがとう、私はそれを働かせました。 – burakongun

0

エラーメッセージには、2つの型を渡す引数として除算演算子を定義していないことが明確に記載されています。コードの抜粋を見ると、これが当てはまります。Matrix<T>を2つ取りますが、いずれもMatrix<T1>Matrix<T2>(異なるタイプの場合はT1T2)を取る演算子があります。

ところで、あなたの質問は何ですか?

関連する問題