IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

problème de redéfinition de classe


Sujet :

Langage C++

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 5
    Points : 3
    Points
    3
    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 : 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
     
     
    #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 : 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
     
    #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!

  2. #2
    Membre expérimenté Avatar de Trademark
    Profil pro
    Inscrit en
    Février 2009
    Messages
    762
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 762
    Points : 1 396
    Points
    1 396
    Par défaut
    Dans le cpp tu ne dois pas remettre la classe en entier. Juste les méthodes :

    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
      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;
      }

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : 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
    #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 : 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
    #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;
      }

  4. #4
    Membre expérimenté Avatar de Trademark
    Profil pro
    Inscrit en
    Février 2009
    Messages
    762
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 762
    Points : 1 396
    Points
    1 396
    Par défaut
    Salut,

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

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Ah génial ça marche parfaitement!
    Merci beaucoup!

  6. #6
    Membre émérite
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    2 764
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 2 764
    Points : 2 704
    Points
    2 704
    Par défaut
    Merci de mettre le fil en "résolu".

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de redéfinition de class
    Par Nieli dans le forum Lisp
    Réponses: 3
    Dernier message: 09/04/2009, 01h46
  2. [css]problème d'attribution de classe dans deux listes
    Par Mitaka dans le forum Mise en page CSS
    Réponses: 9
    Dernier message: 24/11/2005, 19h05
  3. [GRASP] Problème responsabilité d'une classe
    Par Royd938 dans le forum Design Patterns
    Réponses: 3
    Dernier message: 22/06/2005, 16h13
  4. [C#]Problème DirectX SDK & Sprite(Class)
    Par doccpu dans le forum DirectX
    Réponses: 3
    Dernier message: 16/06/2005, 14h30
  5. [MFC] Problème pointeur sur une classe
    Par mick74 dans le forum MFC
    Réponses: 7
    Dernier message: 14/04/2004, 15h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo