Précédent   Forum du club des développeurs et IT Pro > C et C++ > C++ > Langage
Langage Langage C++, Programmation Orientée Objet, Templates, etc. Avant de poster : FAQ C++
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 09/02/2013, 11h53   #1
flashcordon
Invité de passage
 
Inscription : février 2013
Messages : 5
Détails du profil
Informations forums :
Inscription : février 2013
Messages : 5
Points : 1
Points : 1
Par défaut problème de redéfinition de classe

Bonjour à tous!

Je débute en POO et je ne comprend pas vraiment ce qui cloche dans mon programme. Le but est de créer une classe Matrice et de définir des opérations dessus. Problème, mon compilateur me renvoie ceci:

matrice.cc:6:7: erreur: redefinition of ‘class Matrice’
matrice.h:4:7: erreur: previous definition of ‘class Matrice’

j'ai cru comprendre que l'inclusion multiple était protégée grâce aux directives de préprocesseur, mais même avec ça ne marche pas...

Je suppose que le problème vient des constructeurs, mais je ne vois pas où. Si vous avez des suggestions...

Voici mes fichiers:

fichier 'matrice.h'

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
 
#ifndef MATRICE_1_H
#define MATRICE_1_H
 
class Matrice
{
public:
  Matrice(double a[3], double b[3], double c[3]);
  Matrice(double a[3]);
  void affichermatrice() const;
  void affichervecteur() const;
 
private:
  double m_matrice[3][3];
  double m_vecteur[1][3];
};
 
#endif
fichier 'matrice.cc'

Code :
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
 
#include<iostream>
#include"matrice.h"
 
using namespace std;
 
class Matrice //Matrice et vecteur et opérations sur les matrices
{
 
public:
  //Méthode initialiser matrice/Constructeur
  Matrice::Matrice(double a[3], double b[3], double c[3]) //constructeur matrice
  {
    for(int j=0;j<3;j++)
      {
	m_matrice[0][j]=a[j];
	m_matrice[1][j]=b[j];
	m_matrice[2][j]=c[j];
      }
 
    for(int k=0; k<3;k++)
      {
	m_vecteur[0][k]=0;
      }
  }
 
  Matrice::Matrice(double a[3]) //constructeur vecteur
  {
        for(int j=0;j<3;j++)
	  {	m_vecteur[0][j]=a[j]; }
 
  for(int i=0;i<3;i++)
      {
	for(int k=0; k<3;k++)
	  {
	    m_matrice[i][k]=0;
	  }
      }
  }
 
 
  //Méthode afficher matrice
  void Matrice::affichermatrice()
  {
    for(int j=0;j<3;j++)
      {
	cout << m_matrice[0][j] << " " << m_matrice[1][j] << " " << m_matrice[2][j] << endl;
      }
  }
  //Méthode afficher vecteur
  void Matrice::affichervecteur()
  {
    cout << m_vecteur[0][0] << " " << m_vecteur[0][1] << " " << m_vecteur[0][2] << endl;
  }
 
  //Méthode exp(matrice)
  //Méthode produit matriciel
private:
  double m_matrice[3][3];
  double m_vecteur[1][3];
};
Merci de votre réponse!
flashcordon est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/02/2013, 12h10   #2
Trademark
Membre émérite
 
Avatar de Trademark
 
Inscription : février 2009
Messages : 568
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 568
Points : 818
Points : 818
Dans le cpp tu ne dois pas remettre la classe en entier. Juste les méthodes :

Code :
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
  Matrice::Matrice(double a[3], double b[3], double c[3]) //constructeur matrice
  {
    for(int j=0;j<3;j++)
      {
	m_matrice[0][j]=a[j];
	m_matrice[1][j]=b[j];
	m_matrice[2][j]=c[j];
      }
 
    for(int k=0; k<3;k++)
      {
	m_vecteur[0][k]=0;
      }
  }
 
  Matrice::Matrice(double a[3]) //constructeur vecteur
  {
        for(int j=0;j<3;j++)
	  {	m_vecteur[0][j]=a[j]; }
 
  for(int i=0;i<3;i++)
      {
	for(int k=0; k<3;k++)
	  {
	    m_matrice[i][k]=0;
	  }
      }
  }
 
 
  //Méthode afficher matrice
  void Matrice::affichermatrice()
  {
    for(int j=0;j<3;j++)
      {
	cout << m_matrice[0][j] << " " << m_matrice[1][j] << " " << m_matrice[2][j] << endl;
      }
  }
  //Méthode afficher vecteur
  void Matrice::affichervecteur()
  {
    cout << m_vecteur[0][0] << " " << m_vecteur[0][1] << " " << m_vecteur[0][2] << endl;
  }
Trademark est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/02/2013, 13h51   #3
flashcordon
Invité de passage
 
Inscription : février 2013
Messages : 5
Détails du profil
Informations forums :
Inscription : février 2013
Messages : 5
Points : 1
Points : 1
Merci pour cette réponse!

Après quelques corrections et un fichier de test pour afficher une matrice, le compilateur me lance maintenant la liste d'erreurs suivantes:

g++ matrice.cc matrice.cc test_matrice.cc -o matrice
test_matrice.cc: In function ‘int main()’:
test_matrice.cc:9:12: attention : extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
test_matrice.cc:9:23: attention : extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
test_matrice.cc:9:34: attention : extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
test_matrice.cc:9:45: erreur: no matching function for call to ‘Matrice::Matrice(<brace-enclosed initializer list>, <brace-enclosed initializer list>, <brace-enclosed initializer list>)’
test_matrice.cc:9:45: note: candidates are:
matrice.h:8:3: note: Matrice::Matrice(double*)
matrice.h:8:3: note: candidate expects 1 argument, 3 provided
matrice.h:7:3: note: Matrice::Matrice(double*, double*, double*)
matrice.h:7:3: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘double*’
matrice.h:4:7: note: Matrice::Matrice(const Matrice&)
matrice.h:4:7: note: candidate expects 1 argument, 3 provided

Que signifient ces erreurs, s'il vous plaît?

Mes fichiers:

'test_matrice.cc'

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#include<iostream>
#include"matrice.h"
 
using namespace std;
 
int main()
{
 
  Matrice A({1.,5.,4.},{2.,3.,4.},{8.,8.,9.});
  A.affichermatrice();
 
  return 0;
}
'matrice.h'

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef MATRICE_1_H
#define MATRICE_1_H
 
class Matrice
{
public:
  Matrice(double a[3], double b[3], double c[3]);
  Matrice(double a[3]);
  void affichermatrice();
  void affichervecteur();
 
private:
  double m_matrice[3][3];
  double m_vecteur[1][3];
};
 
#endif
'matrice.cc'

Code :
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
#include<iostream>
#include"matrice.h"
 
using namespace std;
 
  //Méthode initialiser matrice/Constructeur
  Matrice::Matrice(double a[3], double b[3], double c[3]) //constructeur matrice
  {
    for(int j=0;j<3;j++)
      {
	m_matrice[0][j]=a[j];
	m_matrice[1][j]=b[j];
	m_matrice[2][j]=c[j];
      }
 
    for(int k=0; k<3;k++)
      {
	m_vecteur[0][k]=0;
      }
  }
 
  Matrice::Matrice(double a[3]) //constructeur vecteur
  {
        for(int j=0;j<3;j++)
	  {	m_vecteur[0][j]=a[j]; }
 
  for(int i=0;i<3;i++)
      {
	for(int k=0; k<3;k++)
	  {
	    m_matrice[i][k]=0;
	  }
      }
  }
 
 
  //Méthode afficher matrice
  void Matrice::affichermatrice()
  {
    for(int j=0;j<3;j++)
      {
	cout << m_matrice[0][j] << " " << m_matrice[1][j] << " " << m_matrice[2][j] << endl;
      }
  }
  //Méthode afficher vecteur
  void Matrice::affichervecteur()
  {
    cout << m_vecteur[0][0] << " " << m_vecteur[0][1] << " " << m_vecteur[0][2] << endl;
  }
flashcordon est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/02/2013, 15h22   #4
Trademark
Membre émérite
 
Avatar de Trademark
 
Inscription : février 2009
Messages : 568
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 568
Points : 818
Points : 818
Salut,

C'est parce que tu dois faire comme ça :

Code :
1
2
3
double a[3] = {1,5,4};
// idem b,c
 Matrice A(a, b, c);
Ou alors activer l'option C++11, si tu peux fait-le, c'est quand même plus beau comme tu as fait toi Mais il y a quand même un truc que j'oublie de préciser c'est qu'il te faudra faire des constructeurs spécifiques, ce qui peu être un peu complexe pour toi pour l'instant.
Trademark est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/02/2013, 15h52   #5
flashcordon
Invité de passage
 
Inscription : février 2013
Messages : 5
Détails du profil
Informations forums :
Inscription : février 2013
Messages : 5
Points : 1
Points : 1
Ah génial ça marche parfaitement!
Merci beaucoup!
flashcordon est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/02/2013, 16h17   #6
oodini
Membre Expert
 
Inscription : novembre 2004
Messages : 2 073
Détails du profil
Informations forums :
Inscription : novembre 2004
Messages : 2 073
Points : 1 458
Points : 1 458
Merci de mettre le fil en "résolu".
__________________
VDS "The C++ Standard Library" (Josuttis) -> 30 €
oodini est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 08h17.


 
 
 
 
Partenaires

Hébergement Web