To whom may be able to lend me a hand,
I am trying to write a code for my C++ class which basically fixes the language's problem of accessing elements out of bounds for a particular buffer using classes and operator overloading. It sounds like basic stuff once you know what's what, and that's the reason why I don't understand why I'm having trouble with this. Here is my code:
The code was conceived in french, but basically replace "tableau" by "buffer" in your head, and you will be fine. The class monTableau should be able to let me create buffers with custom index numbers (defined by the user in borneInf and borneSup). I won't detail it too much here, but in case someone would feel comfortable helping out, it would be my pleasure to hook up on skype and look at it together.
edit: im adding comments to the code as I find the problematic lines.
I am trying to write a code for my C++ class which basically fixes the language's problem of accessing elements out of bounds for a particular buffer using classes and operator overloading. It sounds like basic stuff once you know what's what, and that's the reason why I don't understand why I'm having trouble with this. Here is my code:
Code:
#include <iostream>
using namespace std;
// création du patron de classe
template <class Type> class monTableau
{
private:
Type *Ptr; //pointeur sur un tableau
int borneInf; // borne inférieure des indices du tableau
int borneSup; // borne supérieure des indices du tableau
public:
monTableau<Type>(int inf, int sup) // Constructeur de tableau
{
Ptr += inf; // Le pointeur avance (ou recule) jusqu'a la borneInf donnée par l'usager
borneInf = inf;
borneSup = sup;
// Test si les bornes inf et sup sont en ordre croissant
if (inf > sup)
{
cout << "Les indices du tableau doivent suivre un ordre croissant." << endl;
}
// Si les bornes suivent un ordre croissant, on remplit le tableau d'éléments vides
else
{
for (int i=inf ; i < sup ; i++)
{
*(Ptr+i)=0; [COLOR="Red"]// edit: This line is supposedly a big part of the problem based on testing[/COLOR]
}
}
}
// Surdéfinition de l'opérateur [], permet d'éviter les débordements de tableaux
Type operator [] (const int n)
{
// Si le n demandé ne fait pas partie des indices du tableau
if (n < borneInf || borneSup < n)
{
cout << "Débordement de tableau, ne peut pas accéder à l'élément " << n << "." << endl;
}
// Si on n'a pas retourné de message d'erreur, ...
else
{
return *(Ptr + n); // ... on retourne l'élément pointé, Ptr + n
}
}
// Surdéfinition de l'opérateur ==, compare l'élément du tableau Ptr avec l'élément visé (elem).
bool operator == (const Type & elem)
{
if (*this == elem) {return true; cout << "test2" << endl;}
else return false;
}
// Surdéfinition de l'opérateur !=, retourne les valeurs opposées de l'opérateur == surdéfini ci-haut.
bool operator != (const Type & elem)
{
if(*this == elem) return false;
else return true;
}
} ;
int main()
{
monTableau<int> tableau1(3,5);
cout << tableau1[4];
// monTableau<int> tableau2(1,2); [COLOR="Red"] // This line makes it seg fault[/COLOR]
// cout << (tableau1[4]==tableau1[4]) << endl; [COLOR="red"] // This line also makes it seg fault[/COLOR]
return 0;
}
edit: im adding comments to the code as I find the problematic lines.



Comment