Ce code est censé afficher 3 fois "Hello World !" mais il refuse de compiler :
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45 template < typename A, typename B > struct Item { void Do() { cout << "Hello World !" << endl; } }; template < typename A = void, typename B = void > class Function { typedef vector< Item< A, B > > Array; typedef vector< Item< A, B > >::iterator Iterator; Array array; public: Function(); void operator()(); }; template < typename A, typename B > Function< A, B >::Function() { array.push_back(Item< A ,B >()); array.push_back(Item< A ,B >()); array.push_back(Item< A ,B >()); } template < typename A, typename B > void Function< A, B >::operator()() { for(Iterator it = array.begin(); it != array.end(); it++) it->Do(); } int main(int argc, char** argv) { Function< > f; f(); return 0; }
% g++ main.cpp
main.cpp:45: erreur: type `std::vector<Item<A, B>, std::allocator<Item<A, B> > >' is not derived from type `Function<A, B>'
main.cpp:45: erreur: expected `;' before `Iterator'
main.cpp: In member function 'void Function<A, B>::operator()()':
main.cpp:65: erreur: 'Iterator' was not declared in this scope
main.cpp:65: erreur: expected `;' before `it'
main.cpp:65: erreur: 'it' was not declared in this scope
Voici ma configuration :
% g++ -v
Utilisation des specs internes.
Target: i486-linux-gnu
Configuré avec: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu
Modèle de thread: posix
version gcc 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Le plus étrange est que l'utilisation d'une class Item simple (non template) permet au code de compiler :
Si vous avez une idée pour corriger la première version je suis preneur.
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
32
33
34
35
36
37
38
39
40
41
42
43
44 struct Item { void Do() { cout << "Hello World !" << endl; } }; template < typename A = void, typename B = void > class Function { typedef vector< Item > Array; typedef vector< Item >::iterator Iterator; Array array; public: Function(); void operator()(); }; template < typename A, typename B > Function< A, B >::Function() { array.push_back(Item()); array.push_back(Item()); array.push_back(Item()); } template < typename A, typename B > void Function< A, B >::operator()() { for(Iterator it = array.begin(); it != array.end(); it++) it->Do(); } int main(int argc, char** argv) { Function< > f; f(); return 0; }![]()
Partager