destruction non souhaitée
Bonjour
J'ai une situation "complexe" que j'ai simplifié dans un exemple stupide mais qui n'est finalement pas si simple.
J'ai une classe "Tree", qui contient un vecteur de classes "Branch" qui elle même contient un vecteur de "Leaf".
Une quatrième classe MotherNature peut attacher des feuilles à un arbre passé en argument.
void fastenLeaf(Tree t);
Finalement j'ai un dernier fichier qui est ma fonction main.
voyez plutôt :
Leaf.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #pragma once
#include <string>
#include <sstream>
using namespace std;
class Leaf
{
private :
int width, height;
string colour;
public:
Leaf(void);
Leaf(int width, int height, string colour);
~Leaf(void);
string toString();
}; |
Leaf.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
| #include "Leaf.h"
Leaf::Leaf(void)
{
}
Leaf::Leaf(int w, int h, string c)
{
width = w;
height = h;
colour = c;
}
Leaf::~Leaf(void)
{
}
string Leaf::toString()
{
ostringstream oss;
oss << "Leaf : " << width << "x" << height << " : " << colour;
return oss.str();
} |
Branch.h
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
| #pragma once
#include <sstream>
#include <string>
#include <vector>
#include "Leaf.h"
using namespace std;
class Branch
{
private :
int length;
vector<Leaf> vect_leaf;
public:
Branch(void);
Branch(int length);
~Branch(void);
int getLenght();
vector<Leaf> getAllLeafs();
void addLeaf(Leaf leaf);
string toString();
}; |
branch.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 35 36 37 38
| #include "Branch.h"
#include <iostream>
Branch::Branch(void)
{
}
Branch::Branch(int l)
{
length = l;
}
Branch::~Branch(void)
{
}
int Branch::getLenght()
{
return length;
}
vector<Leaf> Branch::getAllLeafs()
{
return vect_leaf;
}
void Branch::addLeaf(Leaf leaf)
{
vect_leaf.push_back(leaf);
std::cout << "1 leaf added" << endl;
}
string Branch::toString()
{
ostringstream oss;
oss << "Branch : " << length << " " << vect_leaf.size() << "leaf(s)";
return oss.str();
} |
Tree.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #pragma once
#include <vector>
#include "Branch.h"
class Tree
{
private :
int height;
vector<Branch> vect_branch;
public:
Tree(void);
~Tree(void);
void addBranch(Branch br);
vector<Branch> getAllBranches();
Branch getSpecificBranch(int length);
int countLeafs();
}; |
Tree.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 35 36 37 38
| #include "Tree.h"
Tree::Tree(void)
{
}
Tree::~Tree(void)
{
}
void Tree::addBranch(Branch b)
{
vect_branch.push_back(b);
}
vector<Branch> Tree::getAllBranches()
{
return vect_branch;
}
Branch Tree::getSpecificBranch(int length)
{
for(size_t i=0 ; i<vect_branch.size() ; i++)
{
if(vect_branch[i].getLenght() == length)
return vect_branch[i];
}
return Branch();
}
int Tree::countLeafs()
{
int nbLeafs = 0;
for(size_t j=0 ; j<vect_branch.size() ; j++)
nbLeafs += vect_branch[j].getAllLeafs().size();
return nbLeafs;
} |
MotherNature.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| #pragma once
#include <iostream>
#include "Tree.h"
class MotherNature
{
public:
MotherNature(void);
~MotherNature(void);
void fastenLeaf(Tree t);
}; |
MotherNature.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include "MotherNature.h"
MotherNature::MotherNature(void)
{
}
MotherNature::~MotherNature(void)
{
}
void MotherNature::fastenLeaf(Tree t)
{
int w, h;
string c;
Branch br = t.getSpecificBranch(10);
std::cout << "width height colour : ";
std::cin >> w >> h >> c;
Leaf lea(w,h,c);
br.addLeaf(lea);
} |
test.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include "Tree.h"
#include "Branch.h"
#include "MotherNature.h"
int main()
{
Tree t;
Branch b1(9), b2(10), b3(11);
t.addBranch(b1);
t.addBranch(b2);
t.addBranch(b3);
MotherNature mn;
mn.fastenLeaf(t);
cout << "Amount of leaves : " << t.countLeafs() << endl;
} |
Le souci c'est qu'à l'affichage j'ai :
width height colour : 10 10 green
1 leaf added
amount of leaves : 0
je suppose donc que la feuille que j'ajoute est détruite ... et ça m'ennuie ...
Merci à ceux qui prendront le temps de me donner un coup de main :D