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

C++ Discussion :

Problème avec les classes génériques


Sujet :

C++

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2007
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2007
    Messages : 236
    Points : 194
    Points
    194
    Par défaut Problème avec les classes génériques
    Bonjour, j'ai un problème avec les classes génériques : en fait j'ai une classe abstraite qui s'appelle "Fourniture" et une classe "Cahier" qui dérive de cette dernière. J'ai créé une classe "Placard" générique qui contient une liste d'éléments avec std::list.
    Mon problème que je n'arrive pas à compiler mon projet sous visual studio et j'ai des erreurs bizarres de type "Error Link".

    Voilà le code de la classe "Fourniture" :
    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
     
    #ifndef Fourniture_H
    #define Fourniture_H
     
    #include<iostream>
    #include <sstream>
     
    class Fourniture
    {
    	protected :
     
    		std::string reference;
    		std::string fournisseur;
    		double prix;
    		std::string afficheBase(); // Méthode accessbile uniquement pour les classes dérivées
    	public :
     
    		Fourniture(const std::string &, const std::string&,double=0.0);
    		Fourniture(const Fourniture &);
    		virtual ~Fourniture();
    		virtual std::string toString()=0;
     
     
     
    };
     
    #endif
     
    #include "Fourniture.h"
     
    Fourniture::Fourniture(const std::string & a, const std::string & b,double c)
    {
    	reference=a;
    	fournisseur=b;
    	prix=c;
    }
    Fourniture::Fourniture(const Fourniture & f)
    {
    	reference=f.reference;
    	fournisseur=f.fournisseur;
    	prix=f.prix;
    }
    Fourniture::~Fourniture()
    {
     
    }
    std::string Fourniture::afficheBase()
    {
    	std::ostringstream ss;
    	ss<<"Reference ="<<reference<<std::endl;
    	ss<<"Fournisseur ="<<fournisseur<<std::endl;
    	ss<<"Prix ="<<prix<<std::endl;
     
    	return ss.str();
    }

    Voilà le code de la classe "Cahier" :

    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
     
    #ifndef Cahier_H
    #define Cahier_H
     
    #include<iostream>
    #include <string>
    #include "Fourniture.h"
     
     
    class Cahier : public Fourniture
    {
    	private :
    		enum typeCahierEnum {quadrille,blanc};
    		int nombrePage;	
    		typeCahierEnum  typeCahier;
     
    	public :
     
    		Cahier(const std::string&, const std::string&,double,int nbpage/*,Cahier::typeCahierEnum*/);
    		Cahier(const Cahier &);
    		virtual ~Cahier();
    		std::string toString();
    		typeCahierEnum getTypeCahier() const {return typeCahier;}
     
     
     
    };
     
    #endif
    #include "Cahier.h"
     
    Cahier::Cahier(const std::string & a, const std::string & b,double c,int d/*,typeCahierEnum e*/) : Fourniture(a,b,c)
    {
    	nombrePage=d;
    //	typeCahier=e;
    }
    Cahier::Cahier(const Cahier & c) : Fourniture(c)
    {
    	nombrePage=c.nombrePage;
    	typeCahier=c.typeCahier;
    }
    Cahier::~Cahier() 
    {
     
    }
    std::string Cahier::toString()
    {
    	std::string str=afficheBase();
    	std::ostringstream ss;
    	ss<<str;
    	ss<<"Nombre Page ="<<nombrePage<<std::endl;
    	ss<<"Type Cahier ="<<typeCahier<<std::endl;
     
    	return ss.str();
    }
    Voilà le code de la classe "Placard" :
    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
    62
    63
    64
    65
    66
    67
    68
    69
    #ifndef Placard_H
    #define Placard_H
     
    #include<iostream>
    #include <string>
    #include <list>
     
    template<class T>
    class Placard 
    {
    	private :
    		long code;
    		bool estOuvert;
    		std::list<T> contenu;
     
    	public :
    		Placard(long code);
    		Placard(const Placard &);
    		virtual ~Placard();
    		void ajouter(T);
    		void afficher();
    		std::string toString();	
     
     
    };
     
    #endif
     
    #include "Placard.h"
     
    template<class T>
    Placard<T>::Placard(long c)
    {
    	code=c;
    	estOuvert=false;//Etat initial
    	contenu=new std::list();
    }
     
    template<class T>
    Placard<T>::Placard(const Placard<T> & c)
    {
    	code=c.code;
    	estOuvert=c.estOuvert;
    	contenu=c.contenu;
    }
    template<class T>
    Placard<T>::~Placard()
    {
     
    }
    template<class T>
    void Placard<T>::ajouter(T )
    {
    	/*if(estOuvert)
    	{
    	}*/
    	contenu.push_back(T);
    }
    template<class T>
    void Placard<T>::afficher()
    {
    	/*if(estOuvert)
    	{
    	}*/
    	std::list<T>::iterator i;
       for(i=L.begin(); i != L.end(); ++i) std::cout << (*i)->toString()<< " ";
       std::cout << std::endl;
     
    }

    Voilà mon programme principal :

    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
     
    #include"Fourniture.h"
    #include"Cahier.h"
    //#include"Crayon.h"
    //#include"BoiteDAgrafes.h"
    #include"Placard.h"
     
    int main()
    {
     
     
    	Cahier *c=new Cahier("ref1","f1",15.5,10);
    	std::cout<<c->toString();
     
    	Placard <Fourniture*> p(12); // Ici j'ai un problème
    	p.ajouter(c); 
    	p.afficher();
     
    	std::list<Fourniture *> L;
    	L.push_back(c);              // Insert a new element at the end
       //L.push_front(0);             // Insert a new element at the beginning
       //L.insert(++L.begin(),2);     // Insert "2" before position of first argument
       //                             // (Place before second argument)
       //L.push_back(5);
       //L.push_back(6);
     
       std::list<Fourniture *>::iterator i;
       for(i=L.begin(); i != L.end(); ++i) std::cout << (*i)->toString()<< " ";
       std::cout << std::endl;
     
    	system("pause");
    	return 0;
    }
    Consultant SharePoint

  2. #2

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

Discussions similaires

  1. Problème avec les classes et les méthodes abstract
    Par BOLARD dans le forum Langage
    Réponses: 5
    Dernier message: 22/09/2007, 20h27
  2. Problème avec les classes HttpWebRequest et HttpWebResponse sous C#
    Par zouzoulikou dans le forum Général Dotnet
    Réponses: 1
    Dernier message: 14/08/2007, 01h46
  3. Problème avec les classes
    Par 30barrett40 dans le forum C++
    Réponses: 6
    Dernier message: 28/03/2007, 14h04
  4. Problème avec une classe générique
    Par Core8 dans le forum C++
    Réponses: 3
    Dernier message: 19/03/2007, 03h18
  5. [POO] PHP5 : Problème avec les classes
    Par fleur_de_rose dans le forum Langage
    Réponses: 9
    Dernier message: 06/05/2006, 19h09

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