constructor called for no reason

hi I'm writing an array list class and whenever I'm adding something, my attributes are reset to zero and my constructor is called multiple times.


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
#if !defined(_TABLEAU___H_)
#define _TABLEAU___H_

#include <assert.h>


#include <iostream>

template <class T>
class Tableau{

  public:
    Tableau(int capacite_initiale=4);
    Tableau(const Tableau&);
    ~Tableau();

    // Ajouter un element à la fin
    void           ajouter(const T& element);
    // Vider le tableau
    void           vider();
    // Retourne le nombre d'éléments dans le tableau
    int            taille() const;

    // Insère element à position index. Les éléments à partir de index sont décalés d'une position.
    void           inserer(const T& element, int index=0);

    // Enlève l'element à position index. Les éléments après index sont décalés d'une position après.
    void           enlever(int index=0);

    // Cherche et retourne la position de l'élément. Si non trouvé, retourne -1.
    int            chercher(const T& element);
    
    //retourne la capacite
    int            getCapacite()const;

    const T&       operator[] (int index) const;
    T&             operator[] (int index);

    bool           operator == (const Tableau<T>& autre) const;
    Tableau<T>&    operator = (const Tableau<T>& autre);

  private:
    T*             elements;
    int            nbElements;
    int            capacite;
};


// ---------- Définitions -------------


template <class T>
Tableau<T>::Tableau(int capacite_)
{
    // À compléter
    //elements = new T[1024]; // cette ligne n'est peut-être pas bonne.

    capacite = capacite_;
    elements = new T[capacite];
    nbElements = 0;

    std::cout<<"mmm\n";

}

template <class T>
void Tableau<T>::ajouter(const T& item)
{
    

    std::cout<<"cap:"<< capacite<<"\n";
    std::cout<<"el:"<<nbElements<<"\n";
    this[nbElements+1] = item;

}



My main
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 <iostream>
#include <fstream>
#include "tableau.h"
using namespace std;
int main(int argc, const char** args)
{
  	
  Tableau<int> t;
  // std::cout << "taille t = " << t.taille() <<std::endl;
  //std::cout << "cap t = " << t.getCapacite() <<std::endl;

  t.ajouter(1);
  cout<<t[0];
  // t.ajouter(2);
  // t.ajouter(3);
  // t.ajouter(4);
  //t.ajouter(5);

  // for (int i= 0; i< t.taille();i++){
    // std::cout << "[" << t[i] << "]";

  // }


  return 0;
  
}


the output:

mmm
cap:4
el:0
mmm
mmm
cap:0
el:0
mmm
mmm
Segmentation fault: 11

thanks in advance!
Last edited on
Look at your ajouter() method.

The first time you call it, which element of your array does that function change the value of?

Which element of the array are you printing out the value of?

As for the rest of your output, are you sure that output matches the code you've posted? Because there's only one call to ajouter() in the code you've posted, and there are clearly two calls made in whichever run generated that output.
ajouter() is supposed to add at the end of the array which is why I nbElememts (who counts the number of added value in my array).

And Yes, this is the output it gives me. earlier it was working correctly but now it's calling my constructor multiple times. Idk if it's important but I'm using g++, and have compiled all the .h and .cpp .
Did not found why it's was making this but I restart from fresh and it didn't call the constructor multiple time. I think it was a g++ problem.

Thanks for your help MikeyBoy.
You're welcome.

Topic archived. No new replies allowed.