Pointeur de fonctions et exceptions
Bonjour a tous,
J'essaie de faire une fonction qui retourne un pointeur de fonction d'une exception.
Ce pointeur sera ensuite géré dans le main pour faire appel à la fonction.
Malheureusement le compilo n'est pas d'accord, et il a du mal avec le type de la fonction (celle qui retourne le pointeur). Bref ,c 'est plus explicite avec du code
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 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
|
void lol() {
cout << "la fonction void\n";
}
int selectionne(int exception) {
if (exception == 1) {
throw 3;
}
else if (exception == 2) {
char *coucou = "coucou";
throw (char*)coucou;
}
else if (exception == 3) {
float unfloat = 1.5;
throw unfloat;
}
else if ( exception == 5) {
void (*pt)() = &lol;
throw pt;
}
else {
throw true;
}
}
char * travaille(void) {
int nb;
cin >> nb;
try {
throw selectionne(nb);
}
catch (int lexception) {
cout << lexception << endl;
}
catch (char *lexception) {
return lexception;
}
catch (float lexception) {
cout << lexception << endl;
char* ok = "ok";
throw ok;
}
}
void (*)()travaille(void) { //ligne 54
int nb;
cin >> nb;
try {
throw selectionne(nb);
}
catch (void (*lexception)()) {
return lexception;
}
} |
ce qu'en dit le compilo:
Code:
1 2 3
|
exception.cpp:54: error: expected unqualified-id before ')' toke
exception.cpp:54: error: expected initializer before "travaille" |
Alors je sais pas si c'est comme ça que ça doit être fait, c'est ma toute première fois avec les pointeurs de fonctions et les exceptions.
Si quelqu'un peut m'éclairer :(