lightrek
lightrek

Reputation: 981

Boost foreach conflicts with Q_FOREACH (Qt) and moc generation?

I have a program edited in Vs 2008 using some libraries such as Qt and Point Cloud Library (PCL).

PCL has a 3rd party library which contains boost.

However, some errors appeared after compiling:

1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012) : error C2061: syntax error : identifier 'tag'

For the first problem, the error location in the source file is:

template<typename SuperMeta,typename TagList>
inline boost::mpl::true_* boost_foreach_is_noncopyable(
  boost::multi_index::detail::random_access_index<SuperMeta,TagList>*&,
  boost::foreach::tag) // <-------------error here for the first compile error.
{
  return 0;
}

I think maybe this indicates that the Q_FOREACH conflicts with the boost foreach.

But I do not know how to solve this problem?

Upvotes: 4

Views: 3271

Answers (2)

cbuchart
cbuchart

Reputation: 11555

The problem is that Qt defines a foreach macro (#define foreach Q_FOREACH) which conflicts with the boost::foreach namespace.

The easiest ways to solve it is to either include Boost before Qt or simply undefine the Qt's macro before including the boost's header file. I prefer the second since it won't need extra documentation (// remember to include Boost before Qt), and it is easier to manage in nested header files and if you use precompiled headers.

#undef foreach
#include <boost/foreach.hpp>

This option is less invasive than disabling Qt's keywords (compilation flag -DQT_NO_KEYWORDS) and can be applied only in the affected files if wanted. It won't affect the use of Q_FOREACH (obviously if you use the Qt's foreach it will fail). It also works independently that Qt is included before or after <boost/foreach.hpp>.

Upvotes: 5

550
550

Reputation: 1140

Setting compiler flag -DQT_NO_KEYWORDS, disables the clash between boost and qt!

But then you need to replace the qt keywords in your code like slots, signals, emit ..., see this post.

(I got this message when introducing boost::multi_index container in my project.)

  • (For qmake projects CONFIG += no_keywords does this.)
  • (For cmake project add_definitions(-DQT_NO_KEYWORDS) does this.)

Upvotes: 1

Related Questions