Classe matrice

pabreto Messages postés 2 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 19 janvier 2008 - 19 janv. 2008 à 18:42
maymouna2008 Messages postés 6 Date d'inscription mardi 8 avril 2008 Statut Membre Dernière intervention 12 avril 2008 - 12 avril 2008 à 19:36
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

 

//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

4 réponses

luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
19 janv. 2008 à 19:08
Elle existe pas la fonction nb_lines, donc pourquoi tu l'appelles ?
0
nickydaquick Messages postés 416 Date d'inscription vendredi 31 janvier 2003 Statut Membre Dernière intervention 19 décembre 2013 3
19 janv. 2008 à 20:16
Salut,
Elle existe vraiment pas ta fonction mon cher
la premiere fois qu'on l'a voit c dans le fichier Matrice.cc a l'implementation de la fonction

Matrice operator + (const Matrice& a, const Matrice& b)

je suis heureux de faire partie d'une grande famille ...!
0
pabreto Messages postés 2 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 19 janvier 2008
19 janv. 2008 à 20:30
merci!
ça marche maintenant (forcément, sans la définir, c'était +dur)

PA
0
maymouna2008 Messages postés 6 Date d'inscription mardi 8 avril 2008 Statut Membre Dernière intervention 12 avril 2008
12 avril 2008 à 19:36
cvous pouvez me donner u_ne solution a ce probleme
constructeur d une matrice declare avec un tableau dynamique pointés sur des rééls?
et merci

amouna
0
Rejoignez-nous