我正在尝试学习STL库,但我遇到了一个奇怪的问题。这段代码可以完美地编译:
void Show(vector<int> myvec)
{
vector<int>::iterator it;
cout << "Vector contains:";
for( it = myvec.begin(); it < myvec.end(); it++)
{
cout << " " << *it;
}
cout << endl;
}
而这个在编译时会给我一条错误消息:
template <class T>
void Show2(vector<T> myvec)
{
vector<T>::iterator it;
cout << "Vector contains:";
for( it = myvec.begin(); it < myvec.end(); it++)
{
cout << " " << *it;
}
cout << endl;
}
错误是:
$ g++ hello.cpp
hello.cpp: In function ‘void Show2(std::vector<T, std::allocator<_Tp1> >)’:
hello.cpp:19: error: expected ‘;’ before ‘it’
hello.cpp:21: error: ‘it’ was not declared in this scope
这似乎是一个非常简单的错误,但我找不到它。
https://stackoverflow.com/questions/5192874
复制相似问题