在 C++ 中访问泛型结构的成员会出错
Accessing members of generic struct in C++ gives error
我是一个完全的初学者,来自C C++。我编写了以下代码作为简单数据库应用程序的一部分:
#include <cstdint>
template <typename T>
struct entry {
uint64_t ID;
T data;
};
template <typename T>
uint64_t calculate_offset(entry<T> thing) {
return sizeof(uint64_t) * entry<T>.ID;
}
我想访问entry.ID
而不必知道entry.data
的类型。当我尝试编译我的代码时,clang 出现以下错误:
totes.cpp:11:48: error: expected '(' for function-style cast or type construction
如何实现所需的行为?此错误是什么意思?
entry<T> thing
// entry<T> is the type
// thing is the name of an instance of the type
若要访问类或结构的成员状态,请通过对象的实例进行访问。例如:
thing.ID;
相关文章:
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在 Clang 中使用指向成员的指针时出错
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- 通过多个类访问成员时出错
- 将私有数据成员作为默认参数传递给该类的公共方法时出错
- 类和成员函数模板专用化出错
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 缺少类型说明符 - int 假定 声明静态成员后出错
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 调用指向成员函数的指针时出错
- 修改另一个类的向量成员时出错
- 向模板化静态成员初始化添加和不添加'typename'都会出错
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 推导成员函数包装器的返回类型时出错
- 编译类模板成员函数时出错
- 在 C++ 中访问泛型结构的成员会出错
- 初始化作为结构成员的函数指针时出错
- 类中的const成员出错