如何在C++中聲明靜態常量值? 我想能夠得到常量Vector3 :: Xaxis,但我不應該能夠改變它。C++類中的靜態常量成員
我已經看到了另一大類下面的代碼:
const MyClass MyClass::Constant(1.0);
我試圖執行,在我的課:
static const Vector3 Xaxis(1.0, 0.0, 0.0);
但是我得到的錯誤
math3d.cpp:15: error: expected identifier before numeric constant
math3d.cpp:15: error: expected ‘,’ or ‘...’ before numeric constant
然後我嘗試了一些更類似於我在C#中所做的事情:
static Vector3 Xaxis = Vector3(1, 0, 0);
但是我得到其他錯誤:
math3d.cpp:15: error: invalid use of incomplete type ‘class Vector3’
math3d.cpp:9: error: forward declaration of ‘class Vector3’
math3d.cpp:15: error: invalid in-class initialization of static data member of non-integral type ‘const Vector3’
我班上的重要組成部分,到目前爲止是這樣的
class Vector3
{
public:
double X;
double Y;
double Z;
static Vector3 Xaxis = Vector3(1, 0, 0);
Vector3(double x, double y, double z)
{
X = x; Y = y; Z = z;
}
};
如何實現我想在這裏做什麼?要有一個Vector3 :: Xaxis,它返回Vector3(1.0,0.0,0.0);
[C++在哪裏初始化靜態常量]可能的重複(http://stackoverflow.com/questions/2605520/c-where-to-initialize-static-const) – Troubadour