靜態類成員上的未解析外部符號簡單地說:我有一個類,主要由靜態的公共成員組成,所以我可以將類似的函數組合在一起,這些函數仍然需要從其他類/函數中調用。無論如何,我已經在類公共范圍中定義了兩個靜態的無符號字符變量,當我試圖在同一個類的構造函數中修改這些值時,在編譯時會得到一個“未解決的外部符號”錯誤。class test
{public:
static unsigned char X;
static unsigned char Y;
...
test();};test::test() {
X = 1;
Y = 2;}我對C+不太熟悉,所以對我放松點。為什么我不能這么做?
3 回答

浮云間
TA貢獻1829條經驗 獲得超4個贊
.CPP
enums
class test {public: const static unsigned char X = 1; const static unsigned char Y = 2; ... test();};test::test() {}
.H
.CPP
class test {public: static unsigned char X; static unsigned char Y; ... test();};
unsigned char test::X = 1;unsigned char test::Y = 2;test::test(){ // constructor is empty. // We don't initialize static data member here, // because static data initialization will happen on every constructor call.}

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
__declspec(dllexport)
- 3 回答
- 0 關注
- 505 瀏覽
添加回答
舉報
0/150
提交
取消