Je construis une classe matricielle pour renforcer mes connaissances en C++. Mon opérateur surchargé == ne cesse cependant de renvoyer une erreur de "rejets qualificatifs", ce que je comprends être une violation des règles const de façon ou d'autre, mais je ne peux pas comprendre comment.Règles const C++?
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;
}
L'erreur est renvoyé sur le 'if (data [i] [j]! = Second_matrix (i, j))' line. Donc, juste pour être complet, voici mon opérateur =:
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);
}
En outre, l'opérateur():
template <class T, unsigned int rows, unsigned int cols>
T & Matrix<T,rows,cols>::operator()(int row, int col){
return data[row][col];
}
Conseil @JakeVA: prototype 'const T & operator() (ligne int , int col) const; La fin de 'const' signifie que' * this' est const. – ephemient
c'était tout, merci – jakev