6 pièce(s) jointe(s)
Problème boost shared_ptr et make_shared
Bonjour,
J'ai un projet c++ 11 qui marche avec Visual Studio 2012 ou supérieur.
Je voudrais le faire tourner sur Visual Studio 2010 avant la norme c++ 11.
Voici les fichiers de codes :
Pièce jointe 374275Pièce jointe 374279Pièce jointe 374283Pièce jointe 374285
J'ai le message d'erreur suivant :
Pièce jointe 374288
En C++ 11 le code problématique était le suivant :
Pièce jointe 374292
D'avance merci de votre aide.
Cdt
Merci - autre problème shared_ptr bibliothèque boost c++ avant norme 11
Merci,
Je n'ai plus de message d'erreur mais avec les shared_ptr de boost, le main affiche à la console des listes ToString vides.
Voici les fichiers sources :
personne.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #ifndef PERSONNE_H
#define PERSONNE_H
#include <string>
class Logement;
class Personne
{
std::string identite;
Logement *residence;
public:
Personne(std::string id);
std::string ToString() const;
void Emmenage(Logement *l);
};
#endif // PERSONNE_H |
personne.cpp
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
| #include "personne.h"
#include "logement.h"
#include <sstream>
Personne::Personne(std::string id):identite(id),residence(nullptr)
{
}
void Personne::Emmenage(Logement *l)
{
if(residence!=nullptr)
residence->Depart(this);
residence = l;
if(residence!=nullptr)
residence->Arrivee(this);
}
std::string Personne::ToString() const
{
std::ostringstream flux;
flux << identite;
if(residence!=nullptr)
{
flux << " ("<<residence->ToString()<<")";
}
return flux.str();
} |
logement.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #ifndef LOGEMENT_H
#define LOGEMENT_H
#include <string>
#include <set>
class Personne;
class Logement
{
std::string adresse;
std::set<Personne*> occupants;
public:
Logement(std::string ad);
std::string ToString() const;
void Arrivee(Personne* p);
void Depart(Personne *p);
std::string ListeOccupants() const;
};
#endif // LOGEMENT_H |
ogement.cpp
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
| #include "logement.h"
#include "personne.h"
#include <sstream>
Logement::Logement(std::string ad):adresse(ad)
{
}
std::string Logement::ToString() const
{
return adresse;
}
void Logement::Arrivee(Personne *p)
{
occupants.insert(p);
}
void Logement::Depart(Personne *p)
{
occupants.erase(p);
}
std::string Logement::ListeOccupants() const
{
std::ostringstream flux;
std::set<Personne*> occupants;
for(std::set<Personne*>::iterator i=occupants.begin();i!=occupants.end();++i)
{
flux << (*i)->ToString() << ',';
}
return flux.str();
} |
ville.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #ifndef VILLE_H
#define VILLE_H
#include <set>
#include <string>
#include <boost/smart_ptr/shared_ptr.hpp>
class Logement;
class Ville
{
std::set<boost::shared_ptr<Logement> > logements;
public:
Ville();
std::string ListeLogements() const;
Logement* CreeLogement(std::string adresse);
};
#endif // VILLE_H |
ville.cpp
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
| #include "ville.h"
#include "logement.h"
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <sstream>
Ville::Ville()
{
}
std::string Ville::ListeLogements() const
{
std::ostringstream flux;
std::set<boost::shared_ptr<Logement> > logements;
for(std::set<boost::shared_ptr<Logement>>::iterator i=logements.begin();i!=logements.end();++i)
{
flux << (*i)->ToString() << ',';
}
return flux.str();
}
Logement* Ville::CreeLogement(std::string adresse)
{
boost::shared_ptr<Logement> l = boost::make_shared<Logement>(adresse);
logements.insert(l);
return l.get();
} |
main.cpp
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
| #include <iostream>
#include "ville.h"
#include "personne.h"
#include "logement.h"
using namespace std;
int main()
{
char z;
cout << "Bienvenue dans ma ville !" << endl;
Ville city;
Logement *l1 = city.CreeLogement("15, rue des fleurs");
Personne p1("paul"),p2("john"),p3("georges"),p4("ringo");
p1.Emmenage(l1);
p2.Emmenage(l1);
p3.Emmenage(l1);
p4.Emmenage(l1);
cout << "Occupants de l1 (normalment 4)"<<endl;
cout << l1->ListeOccupants() << endl;
Logement *l2 = city.CreeLogement("23 rue des roses");
p2.Emmenage(l2);
cout << "Affichage d'une personne :"<<p2.ToString() << endl;
cout << "Occupants de l1 (3) :"<<l1->ListeOccupants() << endl;
cout << "Occupants de l2 (1) :"<<l2->ListeOccupants() << endl;
cout << "Liste des logements : ";
cout << city.ListeLogements()<<endl;
cin >> z;
return 0;
} |