作者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 (
[email protected])
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
1F:推 FreeF1y2:喔喔可以了 太感谢您了 140.113.95.47 08/11 23:13
2F:推 FreeF1y2:不过为什麽static成员要另外定义呢 140.113.95.47 08/11 23:15
3F:推 jaw109:定义该成员所属的scope? 我也很想知道... 219.81.199.194 08/12 00:24
4F:推 tinlans:static data member 性质跟 global var 61.230.217.177 08/12 04:54
5F:→ tinlans:类似,程式启动时就要占据一个空间,所以 61.230.217.177 08/12 04:55
6F:→ tinlans:需要在某处定义它,以配置静态空间给它。 61.230.217.177 08/12 04:55