私はC++の知識を強化するために、マトリックスクラスを構築しています。しかし、私のオーバーロードされた==演算子は、何らかの形でconstルールの違反であると理解している「discards qualifiers」エラーを返し続けますが、どのように把握できません。C++のconstルール?
template <class T, unsigned int rows, unsigned int cols>
bool Matrix<T,rows,cols>::operator==(const Matrix<T,rows,cols>& second_matrix) const{
if (_rows != second_matrix.getNumRows() || _cols != second_matrix.getNumCols())
return false;
else{
unsigned int i,j;
for (i = 0; i < rows; i++){
for (j = 0; j < cols; j++){
if (data[i][j] != second_matrix(i,j))
return false;
}
}
}
return true;
}
エラーは 'if(data [i] [j]!= second_matrix(i、j))'行に返されます。 !
template <class T, unsigned int rows, unsigned int cols>
bool Matrix<T,rows,cols>::operator!=(const Matrix<T,rows,cols>& second_matrix) const{
return !(*this == second_matrix);
}
また、()演算子:
template <class T, unsigned int rows, unsigned int cols>
T & Matrix<T,rows,cols>::operator()(int row, int col){
return data[row][col];
}
ヒント@JakeVA:プロトタイプ 'のconst T&演算子()(int型の行コンパイラが自動的にconstのオーバーロードを選択します、int col)const; '末尾の 'const'は' * this'がconstであることを意味します。 – ephemient
それは、ありがとう – jakev