bonjour!
je dois écrire le matrice.c d'une classe de matrice à partir d'un matrice.h que l'on me donne et que je n'a pas le droit de modifier.
voici mes fichiers:
Matrice.h:
class Vecteur{
private:
unsigned int n;
double*p;
public:
Vecteur(int _n):n(_n),p(new double[_n])
{
for (int i=0; i<n;++i)
p[i]=0;
}
Vecteur(const Vecteur&);
Vecteur& operator=(const Vecteur&);
double& operator()(int i) {return p[i];}
double operator()(int i) const {return p[i];}
unsigned int size() const {return n;}
~Vecteur(){delete [] p;}
};
extern Vecteur operator+(const Vecteur&, const Vecteur&);
extern Vecteur operator-(const Vecteur&, const Vecteur&);
extern Vecteur operator*(double,const Vecteur&);
inline Vecteur operator*(const Vecteur& x, double alpha)
{return alpha*x;
}
class Matrice{
private:
unsigned int nx;
unsigned int ny;
double* p;
public:
Matrice(unsigned int _nx, unsigned int _ny);
Matrice( const Matrice& a);
Matrice& operator=(const Matrice&);
double& operator()(int i, int j) {return p[nx*i+j];}
double operator()(int i, int j) const {return nx;}
~Matrice();
};
extern Matrice operator+(const Matrice&,const Matrice&);
extern Matrice operator-(const Matrice&, const Matrice&);
extern Matrice operator*(double , const Matrice&);
inline Matrice operator*(const Matrice& A, double alpha)
{
return alpha*A;
}
extern Matrice operator*(const Matrice&, const Matrice&);
extern Vecteur operator*(const Matrice&, const Vecteur&);
et
Matrice.cc:
#include "matrice.h"
#include <cmath>
#include <iostream>
//creation
Matrice::Matrice(unsigned int _nx,unsigned int _ny)
{
nx=_nx;
ny=_ny;
int dim = _nx * _ny;
p=new double[dim];
}
//remplissage
Matrice::Matrice(const Matrice& a)
{
nx=a.nx;
ny=a.ny;
int dim=nx*ny;
p=new double[dim];
for(int i=0;i<nx;i++)
for(int j=0;j<ny;j++)
p[nx*i+j]=a(i,j);
}
//effacage
Matrice::~Matrice(){delete p; nx=0;ny=0;}
//operateur de recopie
Matrice& Matrice::operator = (const Matrice& a)
{
delete[] p;
nx=a.nx;
ny=a.ny;
int dim=nx*ny;
p=new double[dim];
for(int i=0;i<nx;i++)
for(int j=0;j<ny;j++)
p[nx*i+j]=a(i,j);
return *this;
}
//somme
Matrice operator + (const Matrice& a, const Matrice& b)
{
unsigned int na,ma,nb,mb;
na=a.nb_lines();
ma=a.nb_columns();
nb=b.nb_lines();
mb=b.nb_columns();
Matrice somme(na,mb);
for(int i=0;i<na;i++)
for(int j=0;j<mb;j++)
{
somme(i,j)=a(i,j)+b(i,j);
}
return somme;
}
//difference
Matrice operator - (const Matrice& a, const Matrice& b)
{
unsigned int na,ma,nb,mb;
na=a.nb_lines();
ma=a.nb_columns();
nb=b.nb_lines();
mb=b.nb_columns();
Matrice difference(na,ma);
for(int i=0;i<na;i++)
for(int j=0;j<ma;j++)
{
difference(i,j)=a(i,j)-b(i,j);
}
return difference;
}
//produit matriciel
Matrice operator * (const Matrice& a, const Matrice& b)
{
unsigned int na,ma,nb,mb;
na=a.nb_lines();
ma=a.nb_columns();
nb=b.nb_lines();
mb=b.nb_columns();
Matrice produit(na,mb);
for(int i=0;i<na;i++)
for(int j=0;j<mb;j++)
{
for(int k=0;k<ma;k++)
produit(i,j)+=a(i,k)*b(k,j);
}
return produit;
}
//produit par un scalaire
Matrice operator * (double r,const Matrice& a)
{
unsigned int na,ma;
na=a.nb_lines();
ma=a.nb_columns();
Matrice produit(na,ma);
for(int i=0;i<na;i++)
for(int j=0;j<ma;j++)
{
produit(i,j)=r*a(i,j);
}
return produit;
}
//produit matrice/vecteur
Vecteur operator * (const Matrice& a, const Vecteur& v)
{
unsigned int na,ma;
na=a.nb_lines();
ma=a.nb_columns();
Vecteur produit(ma);
for(int i=0;i<na;i++)
for(int k=0;k<ma;k++)
{
produit(i)+=a(i,k)*v(k);
}
return produit;
}
mais j'ai un problème à la compilation, il me dit que dans toutes mes surdéfinitions d'opérateurs, " 'const class Matrice' has no member named 'nb_lines' " et la même chose pour le nombre de collonnes.
Quelqu'un peut-il m'aider?
Merci!
PA