gpt4 book ai didi

c++ - 在 C++ 中初始化查找表

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:00 25 4
gpt4 key购买 nike

因为我似乎无法使我的转换类的构造函数静态化,所以我尝试伪造它,但它给了我一个关于未解析的外部的错误:

struct FloatConversions {
static std::array<float, 256> ByteLUT;

struct Initializer {
Initializer() {
for (double i = 0; i < 256; i++) {
ByteLUT[i] = i / 255.0;
}
}
};
Initializer Init;

static inline float ByteToFloat(int val) {
return ByteLUT[val];
}
static inline uint8_t FloatToByte(float val) {
return static_cast<uint8_t>(val * 255.0f);
}
};
typedef FloatConversions FC;

可能是什么问题?

最佳答案

这是一个可以完成这项工作的简单解决方案。

float ByteToFloat(int val)
{
static const struct FloatConversions
{
std::array<float, 256> ByteLUT;

FloatConversions()
{
for (int i = 0; i < 256; i++)
{
ByteLUT[i] = i / 255.0f;
}
}
} conveter;

return conveter.ByteLUT[val];
}

关于c++ - 在 C++ 中初始化查找表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161857/

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