编译错误提升throw_exception.hpp

问题描述:

我想为我的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

它看起来像实际的错误在于<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://*.com/questions/2762568/c-include-file-order-best-practices)讨论包括排序。 – 2011-05-03 15:26:40

+0

这个问题的确是标题包含排序。当我发现问题时忘了接受这个答案。 – ssell 2011-07-12 20:38:26

我有完全相同的错误,因为有人认为创建一个名为throw_exception的宏很酷。

如果包含此#define的头文件曾经包含在boost头文件之前,那么这会导致构建失败。这是因为:

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

将“throw_exception”替换为创建无效代码的宏。