在 C++ 中访问泛型结构的成员会出错

Accessing members of generic struct in C++ gives error

本文关键字:成员 出错 结构 泛型 C++ 访问      更新时间:2023-10-16

我是一个完全的初学者,来自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;