c++ - Boost foreach 与 Q_FOREACH (Qt) 和 moc 生成冲突?

标签 c++ qt boost point-cloud-library

我有一个在 Vs 2008 中使用 Qt 和点云库 (PCL) 等库编辑的程序。

PCL 有一个包含 boost 的第 3 方库。

但是编译后出现了一些错误:

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'

第一个问题,源文件中错误位置为:

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;
}

我认为这可能表明 Q_FOREACH 与 boost foreach 冲突。

但是我不知道如何解决这个问题?

最佳答案

问题是 Qt 定义了一个 foreach#define foreach Q_FOREACH 冲突的宏 ( boost::foreach )命名空间。

解决它的最简单方法是在 Qt 之前包含 Boost,或者在包含 boost 的头文件之前简单地取消定义 Qt 的宏。我更喜欢第二种,因为它不需要额外的文档 ( // remember to include Boost before Qt ),而且在嵌套的头文件中管理起来更容易,如果您使用预编译的头文件。

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

此选项比禁用 Qt 的关键字(编译标志 -DQT_NO_KEYWORDS)侵入性更小,并且可以在需要时仅应用于受影响的文件。不会影响Q_FOREACH的使用(显然,如果您使用 Qt 的 foreach,它将失败)。 Qt 包含在 <boost/foreach.hpp> 之前或之后也是独立工作的.

关于c++ - Boost foreach 与 Q_FOREACH (Qt) 和 moc 生成冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686283/

相关文章:

c++ - 如何将 boost 库安装到我的 MinGW 编译器?

c++ - 如何声明可变数量的模板参数?

c++ - Boost.asio 和异步链,unique_ptr?

c++ - 如何测试类型是否是具有非类型参数的模板的特化?

c++ - Stroustrup 的 RAII 和强制转换运算符 FILE*() = 矛盾?

c++ - OpenCV c++ 示例到 iOS 项目

C++ 在 << 作为参数后使用 stringstream

html - 有没有办法在 html 中插入 QPixmap 对象?

qt - QT中的图片控件

qt - 从 QHeaderView saveState/restoreState 中排除移动的列顺序