【问题标题】:Weird linker problem with static const class members静态 const 类成员的奇怪链接器问题
【发布时间】:2011-05-26 13:01:29
【问题描述】:

请告诉我,为什么 gcc 链接器给我以下错误:"test_class::test_struct::constVar",引用自:__ZN12lu_test_class27test_struct6constVar$non_lazy_ptr in test_class.o ?

我的代码(test_class.h):

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

所有对 constVar 的引用都在 test_class 范围内,采用通常的静态成员访问形式:test_struct::constVar

【问题讨论】:

    标签: c++ visual-c++ gcc linker static-members


    【解决方案1】:

    提供类外静态成员的定义

    const int test_class::test_struct::constVar;
    

    This 为我工作。

    【讨论】:

    • 谢谢,普拉松!谁能解释一下为什么有必要?
    • @Ryan:是的!阅读this 答案。 :)
    • 很抱歉,但问题没有解决 - 我得到链接器错误“重复符号 test_class::test_struct::constVar”,即使带有 constVar 定义的头文件只包含一次并且我使用--allow-multiple-definition(以防万一)...请帮助!
    • 没关系,实际上......我认为这个问题的根源是 constVar 在我的情况下不是公共成员。我想知道为什么 gcc 认为一切都很好......
    • 为了避免 GCC 4.2.1 链接器问题,我最终将静态 const 成员重新定位到类范围之外...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多