2011-05-26 22 views
3

請你告訴我,爲什麼GCC鏈接給我下面的錯誤:「爲test_class :: test_struct :: constVar」,從引用:在test_class.o __ZN12lu_test_class27test_struct6constVar $ non_lazy_ptr?奇怪的連接問題靜態常量類成員

我的代碼(test_class.h):

class test_class 
{ 
    struct test_struct 
    { 
     static const int constVar = 0; 
    }; 
}; 

所有對constVar是在通常的靜態成員訪問形式爲test_class範圍:test_struct :: constVar

回答

3

提供靜態成員的定義在類的外部

const int test_class::test_struct::constVar; 

This爲我工作。

+0

謝謝,Prasoon!任何人都可以解釋爲什麼它是必要的? – Ryan 2011-05-26 13:08:58

+0

@Ryan:是的!閱讀[本](http://stackoverflow.com/questions/6106194/definition-of-static-const-outside-the-class-definition/6106240#6106240)的答案。 :) – 2011-05-26 13:09:57

+0

我很抱歉地說,但問題沒有解決 - 即使帶有constVar定義的頭文件只包含一次,我得到鏈接錯誤'重複的符號test_class :: test_struct :: constVar',我使用 - 允許多重定義(以防萬一)...請幫助! – Ryan 2011-05-27 07:13:48