C++模板 t 不是有效的模板类型
C++ template t is not valid template type
我的.h文件:
template <typename T>
class UpdateUtils
{
public:
typedef struct {
QList<T> file;
} TPath;
static TPath *getIdealPath(QList<TPath *> &paths);
};
我的.cpp文件:
template <typename T>
TPath *UpdateUtils<T>::getIdealPath(QList<TPath *> &paths) {
return 0;
}
这会在 cpp 文件中产生错误:
error: C2143: syntax error : missing ';' before '*' error: C2065: 'T' : undeclared identifier error: C2923: 'UpdateUtils' : 'T' is not a valid template type argument for parameter 'T'
如果我TPath *
返回类型替换为例如int
,它有效。你能指教吗?
TPath
是UpdateUtils
内部定义的嵌套类,您应该对其进行限定并使用typename
关键字。
template <typename T>
typename UpdateUtils<T>::TPath *UpdateUtils<T>::getIdealPath(QList<TPath *> &paths)
^^^^^^^^^^^^^^^^^^^^^^^^^
或者按照建议@PiotrSkotnicki应用尾随返回类型:
template <typename T>
auto UpdateUtils<T>::getIdealPath(QList<TPath *> &paths) -> TPath *
^^^^ ^^^^^^^^^^
请注意,对于类定义之外的成员函数定义,将在类范围内查找参数列表和尾随返回类型中使用的名称,因此您无需限定它们(不过可以限定它们(。这不适用于返回类型。[basic.scope.class]/4
扩展到类定义末尾或超过类定义末尾的声明的潜在范围也会扩展到其成员定义定义的区域,即使成员在类外部以词法方式定义(这包括静态数据成员定义、嵌套类定义和成员函数定义,包括成员函数体和此类定义中此类定义中遵循声明符 id 的任何部分, 包括参数声明子句和任何默认参数(。
相关文章:
- C++模板 t 不是有效的模板类型
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 此递归模板类型定义是否有效C++?
- 用于随机数据访问的最有效文件类型
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 从浮点数中删除小数部分但保留类型的有效方法
- 我应该将哪种有效负载类型发送给webrtc::P ayloadRouter的构造函数?
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 张量(多维数组)是树前瞻数据的有效存储类型吗?
- 自定义类型转换运算符在转发引用上调用时不起作用(当对象按值传递时有效)
- 通过 typedef 模板<类型名 T、T> 强制模板实例化 - 为什么有效?
- 为什么重写方法并将 const 添加到参数类型有效
- 这是有效的C++代码吗?(在声明范围之外使用本地类型)
- 如何有效地将底层数据从 std::string 移动到其他类型的变量?
- 作为模板非类型参数的 C 字符串在 gcc 6.3 中有效,但在 Visual Studio 2017(x64 为 19
- 自 C++17 年以来,具有正确地址和类型的指针始终是有效的指针
- 表达式块类型有效 // 堆栈溢出
- 为什么此返回类型有效
- 类型(::x);有效的