#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);
booloperator == (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;
}
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 .