class Matrix {
public:
Matrix(int num_rows, int num_columns);
~Matrix();
int NumRows() const;
int NumColumns() const;
double& Element(int row, int col);
private:
int num_rows_;
int num_columns_;
// We'll use the "flat" representation.
double* elements_;
};
Indices:
rm 1.tar.gz; wget --no-cache http://fabien.viger.free.fr/cpp/td8/1.tar.gz && tar xf 1.tar.gz make 1.1RENDU: 1.1.cc
make 1.3.1puis:
make 1.3.2Vous devriez avoir des erreurs: ça compile bien, mais ça ne tourne pas!
|
|
#include "main_utils.h"
#include "1.4.h"
int main() {
Matrix a(4, 3);
a.Element(2, 1) = 42.0;
a.Element(3, 2) = -3.5;
const Matrix b = a;
cout << "b[2][1] = " << b.Element(2, 1) << endl;
cout << "b[3][2] = " << b.Element(3, 2) << endl;
cout << "PASSED" << endl;
}
RENDUS: 1.4.h, 1.4.cc
[[ 1 2 3.4] [ -2.1 3.14159 -9]] |
#ifndef _1_4_H #define _1_4_H ... ... #endif // _1_4_H
g++ -std=c++11 1.4.cc matrix_ops.cc 1.6.test.cc && ./a.out
RENDUS: matrix_ops.h et matrix_ops.cc mis à jour, et 1.6.test.cc
Matrix x = quelquechose;
x = MatrixProd(x, x);
Pour réparer cette erreur, vous pouvez personaliser l'operator= vous-même, par exemple. Vous pouvez aussi choisir de complètement éviter l'opérateur =, en s'appuyant par exemple sur une fonction
void CopyTo(const Matrix& from, Matrix* to) qui copie
from dans to, en supposant qu'elles ont déja les mêmes
dimensions; fonction que vous ajouteriez vous-même a matrix_ops.h.