无法使用 clang 编译代码,但可以使用 gcc

Cannot compile code with clang, but works with gcc

我正在尝试修复一个开源 C++ 项目中的一些错误,而原作者目前忙于他的学术生活而无法提供帮助。通过 macports 安装的 gcc-4.9 代码编译得很好。我一直在命令行上使用 lldb 进行调试。但是,如果可能的话,我想让代码用 clang 编译,因为这样我就可以同时使用 Xcode 和 lldb 并使错误更容易隔离。

当我尝试用 clang 编译代码时,出现以下错误:

In file included from ./src/include/hash.h:25:
./src/include/hash_stream.h:18:11: error: call to function 'operator>>' that is neither visible in the template definition
  nor found by argument-dependent lookup
  iss >> table[key] ;
      ^
./src/common/tagger/implementations/collins/tagger.h:118:9: note: in instantiation of function template specialization
  'operator>><CWord, english::CTag>' requested here
  i >> (*m_TopTags);
    ^
./src/english/tags.h:29:23: note: 'operator>>' should be declared prior to the call site or in namespace 'english'
inline std::istream & operator >> (std::istream &is, english::CTag &tag) {
                  ^
1 error generated.
make: *** [obj/english.postagger.o] Error 1

这个错误发生在 clang 兼容性指南的 this file which is basically trying to read a key, a :, and a value separated by spaces into a custom hash map. I have read the relevant section 中,但我不知道我需要更改什么才能编译它。

编辑:根据@m-s,我修改了src/common/tagger/implementations/collins/tagger_include.h以将tags.h移动到hash_stream.h之上,这似乎修复了该错误。但是,我现在运行进入了一个新的错误:

In file included from ./src/common/conparser/implementations/muhua/weight.cpp:13:
In file included from ./src/common/conparser/implementations/muhua/weight.h:13:
In file included from ./src/common/conparser/weight_base.h:13:
In file included from ./src/common/conparser/base_include.h:10:
In file included from ./src/english/tags.h:43:
In file included from ./src/english/pos/penn_morph.h:15:
In file included from ./src/include/knowledge/tagdict.h:15:
In file included from ./src/include/hash.h:25:
./src/include/hash_stream.h:15:11: error: call to function 'operator>>' that is neither visible in the template definition
      nor found by argument-dependent lookup
      iss >> key;
          ^
./src/include/learning/perceptron/hashmap_score_packed.h:282:7: note: in instantiation of function template specialization
      'operator>><std::__1::pair<unsigned long, unsigned long>, CPackedScore<double, 2048> >' requested here
   is >> static_cast< CHashMap< K, CPackedScore<SCORE_TYPE, PACKED_SIZE> > &>(score_map) ;
      ^
./src/common/conparser/implementations/muhua/weight.cpp:48:27: note: in instantiation of function template specialization
      'operator>><std::__1::pair<unsigned long, unsigned long>, double, 2048>' requested here
   iterate_templates(file >>,;);
                          ^
./src/common/conparser/implementations/muhua/weight.h:133:4: note: expanded from macro 'iterate_templates'
   left(m_mapS0cmN0tm)right\
   ^
./src/include/pair_stream.h:16:16: note: 'operator>>' should be declared prior to the call site
std::istream & operator >> (std::istream &is, std::pair<T1, T2> &p) {
               ^
1 error generated.
make: *** [obj/english.conparser/weight.o] Error 1

我试图在 hash.h 之前包含 pair_stream.h 但这似乎不起作用,我又被难住了。任何帮助将非常感激。

编辑 2:我实际上考虑了一些事情并再次尝试了这个,现在一切正常了。感谢大家! Whosebug 很棒 :)

operator>> 是为 istringstream 定义的。在定义 namespace english 之前尝试在源代码中包含 #include <sstream> 文件。

如错误消息所述,std::istream & operator >> (std::istream &is, english::CTag &tag)src/english/tags.h 中定义。 但是由于调用它的 src/include/hash_stream.h 中的模板是在包含该文件之前声明的,因此会发生错误。

文档中已经给出了解决方案:

Make sure the function you want to call is declared before the template that might call it. This is the only option if none of its argument types contain classes. You can do this either by moving the template definition, or by moving the function definition, or by adding a forward declaration of the function before the template.

因此,要么确保 包含 src/include/hash_stream.h 之前包含此文件,要么应用文档提供的任何解决方案。

在某些情况下,如果您不能仅仅更改声明的顺序来避免问题 - 您可以 前向声明