【问题标题】:Non-static data member error with clang but not g++带有clang但不是g ++的非静态数据成员错误
【发布时间】:2013-01-14 22:49:10
【问题描述】:

总结: 我有一个code snippet,可以用 g++ 编译,但不能用 clang。

详情

我有一个使用 g++ 编译的项目,但是使用 clang 编译时,我收到关于 error: use of non-static data member 的错误。我尝试创建一个小测试用例来演示问题,但是对于小测试用例,g++ 给出了与 clang 相同的错误。

我已向 pastebin 发布了一个 236 行的文件来演示该问题: http://pastebin.com/DGnfxmYe

当使用 g++ 4.6.3 编译时,它可以正常工作。但是当使用 clang 3.2 编译时,我收到以下错误消息:

myhashmap.hpp:169:29: error: use of non-static data member 'num_bins' of 'MyHashMap' from nested type 'iterator'
          for (_index++; (_index < num_bins) && (bins[_index] == NULL); _index++)
                                   ^~~~~~~~
myhashmap.hpp:169:43: error: use of non-static data member 'bins' of 'MyHashMap' from nested type 'iterator'
          for (_index++; (_index < num_bins) && (bins[_index] == NULL); _index++)
                                                 ^~~~
myhashmap.hpp:171:17: error: use of non-static data member 'num_bins' of 'MyHashMap' from nested type 'iterator'
          if (_index < num_bins) {
                       ^~~~~~~~
myhashmap.hpp:172:17: error: use of non-static data member 'bins' of 'MyHashMap' from nested type 'iterator'
            _theNode = bins[_index];
                       ^~~~

查看代码,我明白为什么 clang 会给出这些错误消息。我不明白的是为什么 g++ 首先正确地编译了代码。我没有写这段代码,但我想让它用clang干净地编译。所以我试图准确地理解它在做什么。我很想了解为什么它用 g++ 编译而不是 clang。 g++ 是否以不同的方式解释 c++ 标准,或者代码是否利用了一些 g++ 扩展?

【问题讨论】:

    标签: g++ clang


    【解决方案1】:

    GCC 4.8 (prerelease) 失败,所以我认为这是一个已修复的错误。不过我找不到相应的错误报告。

    要修复代码,我认为您需要将 int _num_bins 成员添加到迭代器,并将 cotnainer 的 num_bins 传递给 begin()end() 中的迭代器构造函数,因此它存储在每个迭代器对象中。


    (另外,不要为不带参数的函数写 (void),这是可恶的。在 C++ 中,不带参数的函数写为 ()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2017-04-30
      • 2013-09-14
      • 2013-11-12
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多