Salut à tous !
je me remets (difficilement) au C++ après une 20aine d'années. je programme une classe 'matrice'. La surcharge de l'opérateur + ne fonctionne pas correctement. L'opération sur des matrices m, n et p du type
p = m + n; renvoie une erreur : no matching function for call to 'matrice::matrice(matrice)' comme si le compilateur cherchait à appliquer le contructeur de copie au lieu de la fonction operator=.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 // matrice.h #ifndef _Matrice_ #define _Matrice_ #include <iostream> using namespace std; class matrice{ friend ostream& operator<<( ostream& flux, matrice &m); friend matrice operator+(const matrice &m1, const matrice &m2); public : matrice() { _li=0; _col=0; _val = 0;}; matrice(int li, int col); matrice(matrice &m); // cons de copie float& operator()(int li, int col); // setter float operator()(int li, int col) const; // getter matrice& operator=(const matrice &m); int lignes(){ return _li;}; int colonnes(){ return _col;}; private : int _li, _col; float *_val; }; #endif // matrice.cpp #include "matrice.h" matrice::matrice(int li , int col){ _li = li; _col= col; _val = new float[_li * _col]; } matrice::matrice(matrice &m){ _li = m._li; _col = m._col; delete[] _val; int n = _li*_col; _val = new float[n]; for(int i=0; i<n; i++) _val[i] = m._val[i]; } matrice operator+(const matrice &m1, const matrice &m2){ matrice temp(m1._li, m1._col); int n = temp._li * temp._col; for(int i = 0; i<n; i++) temp._val[i] = m1._val[i] + m2._val[i]; return temp; } matrice &matrice::operator=(const matrice &m){ if(this !=&m) { delete [] _val; _li = m._li; _col = m._col; int n = _li*_col; _val = new float[n]; for(int i = 0; i<n; i++) _val[i] = m._val[i]; } return *this; } //main.cpp : #include <iostream> #include "matrice.h" using namespace std; int main() { matrice m(3,3); for(int i = 0; i<3; i++) for(int j=0; j<3; j++) m(i,j) = i*3 + j; matrice n; matrice p; n = m; p = m + n; ! no matching function for call to 'matrice::matrice(matrice)' cout << p << endl; return 0; }
Partager