Bonjour,

Étant donnée que j'utilise souvent les matrices lorsque je programme en C++, j'ai décidé de faire une classe "Matrice" capable de tout faire (addition, multiplication, inverse, transposer etc ...). J'ai besoin d'aide sur certaine choses que je ne sais pas faire et pour améliorer ce que j'ai déjà fait.

Ma classe "Matrice" est générique et doit pouvoir contenir n'importe quelle objet.
Lorsqu'elle contient des nombres (int, double, bool etc ...), on doit pouvoir faire les opérations usuelles sur les matrices.
Je dois pouvoir afficher ma matrice grâce à l'opérateur << (peut importe le type d'objet quelle contient).

Mon code pour l'instant :

Matrice.h
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
// Gardien //---------------------------------------------------------------------------------------------
#ifndef MATRICE_H
#define MATRICE_H
 
// En-tête //---------------------------------------------------------------------------------------------
 
/* !> ------------------------------------------------------------------------------------------------- <!
   !>  Classe : Matrice                                                                                 <!
   !>                                                                                                   <!
   !>  Principe : Classe représentant une matrice.                                                      <!
   !> ------------------------------------------------------------------------------------------------- <! */
// Classe  M a t r i c e //--------------------------------------------------------------------------------
template<class T>
class Matrice {
    private:
        // ------------------------------------------------------------------------------- // Attributs //
        unsigned int _nbrLig, // Nombre de lignes.
                     _nbrCol, // Nombre de colonnes.
                     _nbrElt; // Nombre total d'éléments (lignes x colonnes).
        T * _matrice;
 
    public:
        // ---------------------------------------------------------------------- // Méthodes publiques //
        Matrice();
        Matrice(unsigned int inNbrLig, unsigned int inNbrCol);
        Matrice(const Matrice<T> & inMatrice);
        ~Matrice();
 
        T operator()(unsigned int i, unsigned int j) const;
        T operator()(unsigned int indice) const;
        T & operator()(unsigned int i, unsigned int j);
        T & operator()(unsigned int indice);
        void operator=(const Matrice<T> & inMatrice);
 
        // --------------------------------------------------------------------------------- // Getters //
        unsigned int getNbrLig() const;
        unsigned int getNbrCol() const;
        unsigned int getNbrElt() const;
};
 
// Fin //-------------------------------------------------------------------------------------------------
#endif // MATRICE_H
Matrice.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// En-tête //---------------------------------------------------------------------------------------------
#include "Matrice.h"
#include <stdexcept>
 
// ----------------------------------------------------------------------------------- // Constructeurs //
template<class T>
Matrice<T>::Matrice() :
    _nbrLig(0), _nbrCol(0), _nbrElt(0)
{
    _matrice = NULL;
}
 
template<class T>
Matrice<T>::Matrice(unsigned int inNbrLig, unsigned int inNbrCol) :
    _nbrLig(inNbrLig), _nbrCol(inNbrCol), _nbrElt(inNbrLig * inNbrCol)
{
    try {
        _matrice = new T[_nbrElt];
    }
    catch (std::bad_alloc & ba) {
        std::cerr << "bad_alloc caught : " << ba.what() << "\n";
    }
}
 
template<class T>
Matrice<T>::Matrice(const Matrice<T> & inMatrice) {
    _nbrLig = inMatrice.getNbrLig();
    _nbrCol = inMatrice.getNbrCol();
    _nbrElt = _nbrLig * _nbrCol;
 
    try {
        _matrice = new T[_nbrElt];
        for (unsigned int i = 0; i < _nbrElt; ++i) {
            _matrice[i] = inMatrice(i);
        }
    }
    catch (std::bad_alloc & ba) {
        std::cerr << "bad_alloc caught : " << ba.what() << "\n";
    }
}
 
// ------------------------------------------------------------------------------------- // Destructeur //
template<class T>
Matrice<T>::~Matrice() {
    delete[] _matrice;
}
 
// ---------------------------------------------------------------------------------------- // Méthodes //
template<class T>
T Matrice<T>::operator()(unsigned int i, unsigned int j) const {
    try {
        return _matrice[_nbrCol * i + j];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
T & Matrice<T>::operator()(unsigned int i, unsigned int j) {
    try {
        return _matrice[_nbrCol * i + j];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
T Matrice<T>::operator()(unsigned int indice) const {
    try {
        return _matrice[indice];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
T & Matrice<T>::operator()(unsigned int indice) {
    try {
        return _matrice[indice];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
void Matrice<T>::operator=(const Matrice<T> & inMatrice) {
    _nbrLig = inMatrice.getNbrLig();
    _nbrCol = inMatrice.getNbrCol();
    _nbrElt = _nbrLig * _nbrCol;
 
    delete[] _matrice;
    try {
        _matrice = new T[_nbrElt];
        for (unsigned int i = 0; i < _nbrElt; ++i) {
            _matrice[i] = inMatrice(i);
        }
    }
    catch (std::bad_alloc & ba) {
        std::cerr << "bad_alloc caught : " << ba.what() << "\n";
    }
}
 
// ----------------------------------------------------------------------------------------- // Getters //
template<class T> unsigned int Matrice<T>::getNbrLig() const { return _nbrLig; }
template<class T> unsigned int Matrice<T>::getNbrCol() const { return _nbrCol; }
template<class T> unsigned int Matrice<T>::getNbrElt() const { return _nbrElt; }
 
// Fin //-------------------------------------------------------------------------------------------------
Je bloque pour écrire une fonction efficace capable d'afficher une matrice. J'ai besoin de cette fonction pour vérifier mon code.

Merci.