我有一个代表 3D 向量的
Vec
结构,我想让它成为静态成员 Zero
constexpr.
struct Vec
{
double X;
double Y;
double Z;
static const Vec Zero; // defined in it's own .cpp file
// ... some functions and operators
};
我尝试了以下方法,但是这段代码无法使用
incomplete type is not allowed
编译。
struct Vec
{
double X;
double Y;
double Z;
static constexpr Vec Zero{ 0.0, 0.0, 0.0 };
};
然后我尝试将定义移出结构。这会编译,但在链接阶段失败,出现
duplicate symbol Vec::Zero
错误。 (Vec
在很多.cpp文件包含的.h文件中)
struct Vec
{
double X;
double Y;
double Z;
static const Vec Zero;
};
constexpr Vec Vec::Zero{ 0.0, 0.0, 0.0 }; // this definition is in .h file, not in .cpp
制作
Vec::Zero
constexpr 的正确方法是什么?有可能吗?