作者tinlans ( )
站內Programming
標題Re: [問題] class有static變數編譯不過
時間Sat Aug 11 23:06:43 2007
※ 引述《FreeF1y2 (FreeF1y2)》之銘言:
: 我用g++ 3.4.6編譯以下程式碼
: class TestClass {
: public:
: static int a;
: };
: int main() {
: TestClass c;
: c.a = 3;
: }
int TestClass::a; // 要在外面補上定義式。
: /var/tmp//cc2UNSTJ.o(.text+0x1e): In function `main':
: : undefined reference to `TestClass::a'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
要學會看懂 error message 才行,
這裡就是 a 沒定義所造成的 linking-time error。
--
Ling-hua Tseng (uranus@it.muds.net)
Department of Computer Science, National Tsing-Hua University
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Software pipelining for VLIW architectures
Homepage:
https://it.muds.net/~uranus
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.225.100
推 FreeF1y2:喔喔可以了 太感謝您了 140.113.95.47 08/11 23:13
推 FreeF1y2:不過為什麼static成員要另外定義呢 140.113.95.47 08/11 23:15
推 jaw109:定義該成員所屬的scope? 我也很想知道... 219.81.199.194 08/12 00:24
推 tinlans:static data member 性質跟 global var 61.230.217.177 08/12 04:54
→ tinlans:類似,程式啟動時就要佔據一個空間,所以 61.230.217.177 08/12 04:55
→ tinlans:需要在某處定義它,以配置靜態空間給它。 61.230.217.177 08/12 04:55