Bonjour,

Je viens du monde JAVA et je commence à me mettre sérieusement au C++ et donc je fais des exercices donnés par le prof en C++ au lieu de java pour m'habituer au langage(dont j'ai déjà la base).

Ici le but est de faire un ensemble de int en se basant sur l'objet bitset.
Tout à l'air de marcher mais quand j'emploie la méthode "BitSetOfShort::toString()" le programme crash en voulant en sortir.
Avec le debugging j'ai vue que le crash arrive dans free.c qui sert à libérer la mémoire des pointeurs mais j'ai du mal à voire mon erreur.

Voici mes classes :
BitSetOfShort :
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
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <bitset>
#include <sstream>
using namespace std;
 
class BitSetOfShorts
{	
public:
	BitSetOfShorts();
	~BitSetOfShorts();
	bool isEmpty();
	int size();
	void add(short e);
	void remove(short e);
	bool contains(short e);
	void unionSets(BitSetOfShorts s);
	void intersectionSets(BitSetOfShorts s);
	string toString();
	bitset<1+SHRT_MAX-SHRT_MIN> getBitset();
 
	static int indexFromElt(short e);
	static short eltFromIndex(int i);
 
	static const short MIN = SHRT_MIN;
	static const short MAX = SHRT_MAX;
private :
	bitset<1+SHRT_MAX-SHRT_MIN> b;
};
CPP
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef BitSetOfShorts_h
#define BitSetOfShorts_h
#include "BitSetOfShorts.h"
#endif
#include "BitSetOfShortsItr.h"
 
/* value : -X.... 0 .... +X */
/* index :  0     X         */
 
BitSetOfShorts::BitSetOfShorts(){}
 
BitSetOfShorts::~BitSetOfShorts()
{
}
 
int BitSetOfShorts::indexFromElt(short e)
{
	return e+1+MAX;
}
 
short BitSetOfShorts::eltFromIndex(int i)
{
	return i-1-MAX;
}
 
bool BitSetOfShorts::isEmpty()
{
	return b.none();
}
 
int BitSetOfShorts::size()
{
	return b.count();
}
 
void BitSetOfShorts::add(short e)
{
	b.set(e+1+MAX,true);
}
 
void BitSetOfShorts::remove(short e)
{
	b.set(e+1+MAX,false);
}
 
bool BitSetOfShorts::contains(short e)
{
	return b.at(e+1+MAX);
}
 
void BitSetOfShorts::unionSets(BitSetOfShorts s)
{
	size_t found;
	string str = b.to_string();
	found = str.find("1");
	while(found!=string::npos)
	{
		b.set(eltFromIndex(-(static_cast<int>(found))-1),false);
		found = str.find("1",found+1);
	}
}
 
void BitSetOfShorts::intersectionSets(BitSetOfShorts s)
{
	size_t found;
	string str = b.to_string();
	found = str.find("1");
	while(found!=string::npos)
	{
		if(!this->contains(found))
		{
			b.set(eltFromIndex(-(static_cast<int>(found))-1),false);
		}
		found = str.find("1",found+1);
	}
} 
 
bitset<1+SHRT_MAX-SHRT_MIN> BitSetOfShorts::getBitset()
{
	return b;
}
 
string BitSetOfShorts::toString() { 
    stringstream r;
	r<<"{";
    BitSetOfShortsItr itr(this);
	if (isEmpty()) return ("{}");
    while (itr.hasMoreElements()){
		r << ", " << itr.nextElement();
		cout<<r.str()<<endl;
    }
	r << "}";
	itr.~BitSetOfShortsItr();
	return r.str();
  }
BitSetOfShortsItr (iterateur de l'ensemble) :
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 BitSetOfShorts_h
#define BitSetOfShorts_h
#include "BitSetOfShorts.h"
#endif
 
class BitSetOfShortsItr
{
public:
	BitSetOfShortsItr(BitSetOfShorts* b);
	~BitSetOfShortsItr();
 
	bool hasMoreElements();
	short nextElement();
private:
	BitSetOfShorts* b;
	bitset<1+SHRT_MAX-SHRT_MIN> bitset;
	int current;
};
CPP
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
#include "BitSetOfShortsItr.h"
 
BitSetOfShortsItr::BitSetOfShortsItr(BitSetOfShorts* b2)
{
	b = b2;
	bitset=b->getBitset();
	current = -1;
}
 
BitSetOfShortsItr::~BitSetOfShortsItr()
{delete b;
~bitset;}
 
bool BitSetOfShortsItr::hasMoreElements()
{
	string::size_type found;
	string str = bitset.to_string();
 
	found = str.find("1", current+1);
 
	if(found!=string::npos)
		return true; 
	else
		return false;
}
 
//PRE : hasMoreNextElements == true
short BitSetOfShortsItr::nextElement()
{
	string::size_type found;
	string str = bitset.to_string();
	found = str.find("1", current+1);
	current = found;
	return b->eltFromIndex(-(static_cast<int>(found))-1);
}
main.cpp :
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
#include <iostream>
#include <stdio.h>
#include <math.h>
#ifndef BitSetOfShorts_h
#define BitSetOfShorts_h
#include "BitSetOfShorts.h"
#endif
#include "BitSetOfShortsItr.h"
 
using namespace std;
 
int main()
{
    BitSetOfShorts a;
    BitSetOfShorts b;
    short ta[6] = {-3, 5, 6, -3, 9, 9};
    short tb[4] = {6, 7, -2, -3};
    int i;
    for (i=0; i<(sizeof(ta)/sizeof(short)); i++) {
	a.add(ta[i]);
	cout << a.toString() <<a.size() << endl;
    }
    for (i=0; i<sizeof(tb)/sizeof(short); i++) {
        b.add(tb[i]);
	cout<<b.toString()<<b.size()<<endl;
    }
    a.unionSets(b);
    cout<<a.toString()<<a.size()<<endl;
 
    return 0;
}
Est-ce que vous pourriez m'aider ?

Cordialement,

rXp>!<