gpt4 book ai didi

c++ - 在 C++ 中访问通用结构的成员会出错

转载 作者:行者123 更新时间:2023-11-28 05:39:29 28 4
gpt4 key购买 nike

我是 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;

关于c++ - 在 C++ 中访问通用结构的成员会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468481/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com