<分区>
现在我正在学习模板和 vector 。我做了一个简单的函数来打印一个 vector ,该 vector 具有从 .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)
我在这里没有看到语法错误。函数上方有一个模板类型名声明。 vector 是 const
通过引用传递以避免复制它,因此函数不会无意中更改 vector 。我有一个常量反向迭代器指向 .back()
元素。然后我取消对迭代器的引用并递增它,直到它到达 vector 的反向端并结束。我正在使用 auto
,因为 vector 可以有任何数据类型。
顺便说一下这个错误怎么读?这是什么意思?请不要太苛刻,因为这对我来说是一个相对较新的话题。我真的很想学习模板和序列容器。