I defined a class template and function templates. I hope to print all types with the funciton:
template <typename T> void MyQueue<T>::Print()
{
std::vector<int>::iterator It1;
It1 = data.begin();
for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
cout << " " << *It1<<endl;
}
by using vector template. Now it just works for int. How can I change it to work for typename T
std::vector<typename T>::iterator It1;
always failed.
Thank you!
/////////////////////////////////////////
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template.cpp: In member function ‘void MyQueue<T>::Print()’:
template.cpp:28: error: expected `;' before ‘It1’
template.cpp:29: error: ‘It1’ was not declared in this scope
template.cpp: In member function ‘void MyQueue<T>::Print() [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’:
template.cpp:55: instantiated from here
template.cpp:28: error: dependent-name ‘std::vector<T,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
template.cpp:28: note: say ‘typename std::vector<T,std::allocator<_CharT> >::iterator’ if a type is meant