2011-05-03 132 views
1

我想爲我的C++項目使用Boost的lexical_cast,但運行到使用Visual Studio 2010 Professional的編譯錯誤。編譯錯誤提升throw_exception.hpp

錯誤如下:

1> VLGUI_Frame.cpp 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(54): error C2143: syntax error : missing ')' before 'constant' 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(54): error C2143: syntax error : missing ';' before 'constant' 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(54): error C2988: unrecognizable template declaration/definition 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(54): error C2059: syntax error : 'constant' 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(54): error C2059: syntax error : ')' 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(72): error C2143: syntax error : missing ';' before '{' 
1>c:\users\dev\external\boost_1_46_1\boost\throw_exception.hpp(72): error C2447: '{' : missing function header (old-style formal list?) 
1> 
1>Build FAILED. 

這裏是使用lexical_cast的代碼(這是沒有關係的,但誰知道它可以幫助)

#include "boost/lexical_cast.hpp" 

... 

std::string Frame::toString() 
{ 
    std::string str = ""; 

    try 
    { 
     str = VLString::combine(12, 
           m_Name.c_str(), 
           " : Dimensions[", 
           boost::lexical_cast<std::string>(m_Rect.width).c_str(), 
           ",", 
           boost::lexical_cast<std::string>(m_Rect.height).c_str(), 
           "] : Loc[", 
           boost::lexical_cast<std::string>(m_Rect.x).c_str(), 
           ",", 
           boost::lexical_cast<std::string>(m_Rect.y).c_str(), 
           "] : NumChildren[", 
           boost::lexical_cast<std::string>(m_Children.size()).c_str(), 
           "]"); 
    } 
    catch(boost::bad_lexical_cast &) 
    { 
     str = VLString::combine(2, 
           m_Name.c_str(), 
           " : lexical_cast failed"); 
    } 

    return str; 
} 

不幸的是我沒有足夠的經驗來幫助Boost自己診斷這個問題。我做了必須的谷歌,沒有結果。

謝謝你的幫助。

+0

雖然在Boost中可能存在嚴重的構建錯誤,但這種情況極不可能...... – 2011-05-03 04:40:45

回答

7

它看起來像實際的錯誤在於<boost/throw_exception.hpp>之前的標題。例如。你的翻譯單元包含有類似

#include "myheader.hpp" 
#include <boost/throw_exception.hpp> 

//You translation-unit specific code in here 

其中"myheader.hpp"包含有類似

class MyClass 
{ 
    //Members 
} // <-- Note missing semicolon! 

一般來說,如果你甚至不能編譯頭文件,你會得到一個error C2447: '{' : missing function header (old-style formal list?),你通常需要檢查發生錯誤之前的標題。

最後,您可以通過在每個翻譯單元內使用標準化標題包含順序來消除此問題。我通常使用這個命令:

  • C標準庫
  • C++標準庫
  • C第三方庫
  • C++第三方庫
  • 我的頭

如果您使用此如果它存在,錯誤會顯示在你自己的代碼中,而不是在你無法控制的第三方頭文件中。

+0

在C++之前包含標準C庫還是隻是爲了整潔? – 2011-05-03 15:23:25

+0

Nevermind,發現這個問題(http://stackoverflow.com/questions/2762568/c-include-file-order-best-practices)討論包括排序。 – 2011-05-03 15:26:40

+0

這個問題的確是標題包含排序。當我發現問題時忘了接受這個答案。 – ssell 2011-07-12 20:38:26

0

我有完全相同的錯誤,因爲有人認爲創建一個名爲throw_exception的宏很酷。

如果包含此#define的頭文件曾經包含在boost頭文件之前,那麼這會導致構建失敗。這是因爲:

template<typename E> BOOST_ATTRIBUTE_NORETURN inline void throw_exception(E const & e) 

將「throw_exception」替換爲創建無效代碼的宏。