【发布时间】:2022-01-12 22:39:34
【问题描述】:
我正在尝试创建一个结构来在头文件中的类的声明部分中保存文件头。这涉及计算编译时已知的值,我想用它来调整标头中的数组大小。
这是我最近尝试的头文件的摘录:
const uint hFreeSiz = (1024 - sizeof(uint32_t) - sizeof (uint16_t)- sizeof (uint16_t) - sizeof(RId))/sizeof (RId);
template <const uint freedSiz>
struct FBHead
{
/** A magic number for the file. Indicates both class and version. */
uint32_t fSig;
/** The number of data bytes within each block. */
uint16_t blkSize;
/** The number of records available for reuse. */
uint16_t freedCnt;
/** The id number of the last record written to the file. */
RId lstRec;
RId freedRecs[freedSiz];
};
FBHead<hFreeSiz> fbHead;
但它告诉我:
错误:非静态数据成员 'FBFile::hFreeSiz' 的使用无效
我可以手动计算,但我试图了解这里发生了什么 为什么这不起作用。谁能解释一下? (我猜如果我移动了 类外的常量声明,那么它会起作用,但我真的宁愿 让它靠近它用来修改的东西。事实上,如果这是唯一的 回答,然后我可能会计算 手动将其放入 cmets 中,说明为什么 特定的数字。)
【问题讨论】:
-
通过非常小的修改(包括 cstdint,使 RId 成为一个空类),它可以用 VC++ 编译。此外,特别是,您的代码中不存在成员 FBFile::hFreeSiz,因此错误不是由您提供的代码生成的。请分享您试图寻求帮助的代码,而不是发明的东西。
标签: c++ arrays declaration