Can't compile code with clang but works with gcc
I'm trying to fix some bugs in an open source C ++ project and the original author is currently too busy with his academic life to help. The code is compiled with the gcc-4.9
macports installed. And I have debugged with lldb
at the command line. However, if possible, I would like to get the code to compile with clang
, as this will allow me to use Xcode both lldb
together and make the error easier to isolate.
When I try to compile the code with clang
, I get the following error:
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
This error occurs in this file, which is basically trying to read the key :
and space separated value into a custom hashmap. I have read the relevant section in the clang compatibility guide, but I cannot figure out what I need to change in order to compile it.
EDIT : Per @ms, I changed src/common/tagger/implementations/collins/tagger_include.h
to move tags.h
above hash_stream.h
and that seems to fix this error. However, now I am facing a new error:
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
I tried to enable pair_stream.h
before hash.h
but that doesn't seem to work and I'm stumped again. Any help is appreciated.
EDIT 2 : I really thought about things and did another hit and now everything works. Thanks to all! StackOverflow is awesome :)
source to share
As stated in the error message, std::istream & operator >> (std::istream &is, english::CTag &tag)
is defined in src/english/tags.h
. But since the template in src/include/hash_stream.h
that calls it is declared before this file is included, an error occurs.
There is already a solution in the documentation:
Make sure the function you want to call is declared before the template that can 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 direct function declaration in front of the template.
So either make sure that this file is included before including src/include/hash_stream.h
, or apply any of the solutions provided by the documentation.
source to share