Problèmes de classe avec const
Bonjour,
en codant une classe Matrix, j'ai des problème avec des surcharges d'opérateurs.
Voici ladite classe
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include "Utilities.h"
#define MapFunction double (*f)(double)
class Matrix
{
public:
Matrix(void);
Matrix(const Matrix& cpy);
Matrix(double array[3][3]);
double getFromArray(uint x, uint y);
void normalize(void);
void map(MapFunction);
bool operator==(Matrix& m2) const;
Matrix operator^(uint p);
Matrix operator*(Matrix& m2);
double& operator[](const int index);
friend ostream& operator<<(ostream& a, const Matrix& m);
protected:
double prob[3][3];
};
#endif // MATRIX_H_INCLUDED |
Code:
1 2 3 4 5 6
| Matrix::Matrix(const Matrix& cpy)
{
for (uint i(0); i < 3; ++i)
for (uint j(0); j < 3; ++j)
prob[i][j] = cpy.getFromArray(i, j); // error: passing `const Matrix' as `this' argument of `double Matrix::getFromArray(uint, uint)' discards qualifiers|
} |
Code:
1 2 3 4 5 6 7 8 9
| Matrix Matrix::operator*(const Matrix& m2)
{ // error: prototype for `Matrix Matrix::operator*(const Matrix&)' does not match any in class `Matrix'|
double t[3][3];
for (uint i(0); i < 3; ++i)
for (uint j(0); j < 3; ++j)
t[i][j] = getFromArray(i, j) * m2.getFromArray(i, j); // error: passing `const Matrix' as `this' argument of `double Matrix::getFromArray(uint, uint)' discards qualifiers|
Matrix a(t);
return a;
} |
J'obtiens les erreurs mises en commentaires dans le code, que je ne comprends pas, car les arguments en const ne sont pas modifiés.
Merci,
bonne journée !