现在我正在学习模板和向量。我制作了一个简单的函数来打印一个向量,其中包含来自
.back()
元素到
.front()
元素。
template <typename Type>
void printVectorReverse(const vector<Type>& stuff)
{
for (auto it = stuff.crbegin(); it != crend(); ++it) {
cout << *it << endl;
}
}
我正在编译程序,但出现了一个错误:
$ g++ -std=c++11 template_functions.cpp
template_functions.cpp: In function âvoid printVectorReverse(const std::vector<Type>&)â:
template_functions.cpp:66:49: error: there are no arguments to âcrendâ that depend on a template parameter, so a declaration of âcrendâ must be available [-fpermissive]
for (auto it = stuff.crbegin(); it != crend(); ++it) {
^
template_functions.cpp:66:49: note: (if you use â-fpermissiveâ, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
这里没有语法错误。函数上方有一个模板类型名声明。向量是
const
传递引用以避免复制它,这样函数就不会无意中更改向量。我有一个常量反向迭代器指向
后退()
元素。然后我取消对迭代器的引用,并将其递增,直到它到达向量的反端,然后
最后的落差
. 我正在使用
auto
因为向量可以有任何数据类型。
顺便问一下,我该如何阅读这个错误?这是什么意思?请不要这么严厉,因为这对我来说是一个相对新的话题。我真的很想学习模板和序列容器。