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