c++ - g++ "is not a type"错误

标签 c++ gcc templates

编写一个模板函数,我声明:

template <typename T>
T invertible(T const& container, T::size_type startIndex, T::size_type endIndex);

使用 g++ 4.0.1 编译时出现错误:

error: 'T::size_type' is not a type

最佳答案

您需要添加类型名称。

template <typename T>
T invertible(T const& container, typename T::size_type startIndex, typename T::size_type endIndex);

在没有关于类型 T 的任何信息的情况下,编译器需要知道 T::size_type 指定了一个类型。

来自标准第 14.6.2 节:

A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.

关于c++ - g++ "is not a type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1301380/

相关文章:

c++ - 如何在 doxygen 文档中获取我的 .cpp 文件?

c++ - 如何在主题中定位切换按钮

c++ - 如何在 c++20 约束算法中投影 std::tuple

optimization - 有没有办法告诉 GCC 不要优化特定的代码?

c++ - 如何使用可变参数模板在 C++11 中生成左关联表达式(也称为左折叠)?

c++ - 在派生类的构造函数中初始化父类(super class)

gcc - gcc打印警告而不进行编译

c - 为什么允许 gcc 从结构中推测加载?

c++ - 模板参数必须是类型吗?

c++ - 如何将对象数组作为参数传递给模板