Bonjour
Le programme suivant compile et fonctionne correctement
Avec ce main() là aussi
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 // Quelques" #include" inutiles, sans aucun doute ! #include <iostream> #include <fstream> #include <string> #include <iomanip> #include <cmath> #include <Windows.h> #include <cstdlib> using namespace std; void fmap( int (*f)(int ) ) { for(int i = 1; i < 10; ++i){ cout<<f(i)<<", "; } } int main() { auto m_f = [](int x){ return 3*x; }; for(int i = 1; i < 10; ++i){ cout<<m_f(i)<<", "; } cout<<"\n -----\n"; fmap(m_f); return 0; }
Mais si on décommente la ligne 11, il ne compile plus
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 int main() { int a = 3; auto m_f = [a](int x){ return a*x; }; for(int i = 1; i < 10; ++i){ cout<<m_f(i)<<", "; } cout<<"\n -----\n"; // fmap(m_f); return 0; }
Le message d'erreur est
Merci d'éclairer ma lanterneerror: cannot convert 'main()::<lambda(int)>' to 'int (*)(int)' for argument '1' to 'void fmap(int (*)(int))'
PS : Windows seven + CodeBlocks + TDM64-GCC Compiler
Partager