Surcharge de l'opérateur [] ("must be a nonstatic member function")
Bonjour,
J'ai plusieurs fois été confronté à ce problème que je n'arrive pas à résoudre..
Je tente de surcharger un opérateur mais j'ai l'erreur suivante :
erreur: ‘int operator[](LIST, int)’ must be a nonstatic member function
J'ai fait quelque recherches sur internet mais je ne comprend toujours pas cette erreur !
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef DEF_LISTE
#define DEF_LISTE
struct element {
int val;
element *nxt;
};
typedef element * LIST;
void push_t(LIST &lst, const int val);
void push_b(LIST &lst, const int val);
void pop_t(LIST &lst);
void pop_b(LIST &lst);
int get_element(LIST lst, int indice);
int operator[](LIST lst, int indice);
void print_list(const LIST lst);
#endif |
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
|
int get_element(LIST lst, int indice) {
if(lst != NULL) {
if(indice == 0) {
return lst->val;
} else {
return get_element(lst->nxt, indice - 1);
}
} else {
return -1;
}
}
int operator[](LIST lst, int indice) {
return get_element(lst, indice);
} |
Merci d'avance et bonne journée !