2013-03-03 203 views
0

在我的代碼使用boost(1.52)和Qt(4.8.3)和第一編譯錯誤我有如下「的boost :: Q_FOREACH」尚未聲明

D:\qt\4.8.3\x86_64-w64-mingw32\include/QtCore/qglobal.h:2439:21: error: 'boost::Q_FOREACH' has not been declared 

它是一個錯誤?

+1

你可以發佈代碼嗎?這可能是Qt的「foreach」宏與推動之間的衝突。 – Angew 2013-03-03 21:57:44

+0

在Qt中有一些定義可以阻止它定義像「foreach」這樣的「關鍵字」。在.pro文件中查看'CONFIG + = no_keywords'。 – Angew 2013-03-03 21:59:20

+0

問題是沒有我的代碼參與這個錯誤。 – user14416 2013-03-03 21:59:27

回答

2

這可能是由boost和Qt之間的衝突引起的。看到這個post

有人建議在你的項目文件中使用CONFIG + = no_keywords。

但我也看到了一票,解決此問題關閉.. https://svn.boost.org/trac/boost/ticket/6455 所以不知道這是否會幫助您的問題

+0

不使用Qt關鍵字是正確的答案。請參閱https://svn.boost.org/trac/boost/ticket/6131瞭解我的失敗嘗試讓Qt和Boost.Foreach玩得很好的漫長而遺憾的歷史。 – 2013-03-05 07:21:04

3

其他任何人有這個問題,另一種解決方法是讓前升壓包括Qt包括

9

#include <QObject>開始你的.cpp文件。

例如,

#include <boost/foreach.hpp> 
#include <QObject> 
#include <boost/multi_index/hashed_index.hpp> 

...編譯爲error: 'boost::Q_FOREACH' has not been declared而這個;

#include <QObject> 
#include <boost/foreach.hpp> 
#include <boost/multi_index/hashed_index.hpp> 

...編譯得很好。


qglobal.h(所有Qt的頭在內)創建的宏#define foreach Q_FOREACH它擴展像boost::foreach::foonamepsace foreach引用,但只有在宏聲明之後遇到的引用。早期定義宏時,所有參考都以相同的方式進行擴展。

我在示例中使用hashed_index.hpp,因爲它恰巧使用了foreach名稱空間。重命名命名空間很難看,但它不會影響客戶端代碼。