泛型函数反向打印任何矢量,编译器出错
Generic function to print any vector in reverse, compiler error
现在我正在学习模板和向量。我做了一个简单的函数来打印一个向量,该向量具有从.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)
我在这里没有看到语法错误。函数上方有一个模板typename声明。矢量是const
传递引用,以避免复制它,因此函数不会无意中更改矢量。我有一个指向.back()
元素的常量反向迭代器。然后我取消引用迭代器,并将其递增,直到它到达向量的反向端,下降到的末尾。我使用auto
,因为矢量可以具有任何数据类型。
顺便问一下,我该如何解读这个错误?这是什么意思?请不要太苛刻,因为这对我来说是一个相对较新的话题。我真的很想学习模板和序列容器。
错误如下所示:
错误:"crend"没有依赖于模板参数的参数,因此必须有<function]>声明"cred">[-fpermission]
这意味着编译器不知道crend()
是什么。它怀疑它是一个函数,但找不到它的声明。
你错别字了;你需要有stuff.crend()
:
for (auto it = stuff.crbegin(); it != stuff.crend(); ++it)
相关文章:
- 使用 Windows 10 和 MINGW 编译器在 VS Code 上使用 gdb 调试器进行调试时出错
- 泛型函数反向打印任何矢量,编译器出错
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 使用编译器进行外部初始化时出错C++
- 编译器包装可解释函数时出错
- 编译 .cu 与 .cpp:即使没有任何 CUDA 代码,编译器也会出错
- 动态堆栈、编译时出错、dev编译器和g++
- 非常量引用类型的无效初始化中出错.在Linux操作系统中使用c++代码.GCC编译器
- 为什么编译器在定义类似的模板专用化时不会出错?
- 编译器在转发结构声明上出错
- MSVC 编译器错误导致迭代器和友元函数出错
- 编译器尝试删除作为指针的模板类型时出错
- 在 eclipse 中使用 migw 编译器对构建 Tapi3 c++ 示例时出错
- 为什么C编译器在[5]==5[a]时出错
- 在Windows 7中通过GnuWin32使用Microsoft Visual C/C++编译器编译源(.C)文件时出错
- 使用PGI编译器链接Armadillo时出错
- 编译器错误消息错误:令牌之前的预期')' '*'。我的构造函数出错
- g++ 编译器调用函数时出错(向量<向量<int> > &)
- 使用针对Win32的英特尔C++编译器编译Qt时出错,我做错了什么
- 当函数没有显式返回值时,如何强制编译器出错