Witam, mam problem natury czysto składniowo-dociekliwej:
W ten sposób jest źle:
#include <vector>
#include <iostream>
using namespace std;
template<class T>
void pokaz(vector<T>::iterator it) {
cout << *it << ends;
}
int main() {
vector<int> v(10, 10);
vector<int>::iterator it( v.begin() );
while( it != v.end() ) {
pokaz(it);
++it;
}
return 0;
}
błędy wyrzucane przez kompilator (VC++ 8.0):
Warning 1 warning C4346: 'std::vector<T>::iterator' : dependent name is not a type d:\programowanie\visual\dziwne\main.cpp 7
Error 2 error C2146: syntax error : missing ')' before identifier 'it' d:\programowanie\visual\dziwne\main.cpp 7
Error 3 error C2182: 'pokaz' : illegal use of type 'void' d:\programowanie\visual\dziwne\main.cpp 7
Error 4 error C2059: syntax error : ')' d:\programowanie\visual\dziwne\main.cpp 7
Error 5 error C2143: syntax error : missing ';' before '{' d:\programowanie\visual\dziwne\main.cpp 11
Error 6 error C2447: '{' : missing function header (old-style formal list?) d:\programowanie\visual\dziwne\main.cpp 11
W ten sposób jest dobrze:
#include <vector>
#include <iostream>
using namespace std;
template<class T>
void pokaz(typename vector<T>::iterator it) {
cout << *it << ends;
}
int main() {
vector<int> v(10, 10);
vector<int>::iterator it( v.begin() );
while( it != v.end() ) {
pokaz<int>(it);
++it;
}
return 0;
}
Problem mi się udało rozwiązać, ale szczerze mówiąc nie wiem dlaczego pierwszy sposób jest zły [???]