在Boost throw_exception.hpp中编译错误

时间:2011-05-03 04:00:43

标签: c++ visual-studio-2010 boost

我正在尝试将Boost的lexical_cast用于我的C ++项目,但是使用Visual Studio 2010 Professional会遇到编译错误。

错误如下:

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来自行诊断这个问题。我做了必须的google-ing而没有结果。

感谢您的帮助。

2 个答案:

答案 0 :(得分: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 ++第三方库
  • 我的标题

如果您使用此订单,则错误将显示在您自己的代码中(如果存在),而不是在您无法控制的某些第三方标题的内容中。

答案 1 :(得分:0)

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

如果包含此#define的标头曾被包含在boost标头之前,则会导致构建失败。这是因为:

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

会有&#34; throw_exception&#34;替换为宏创建无效代码。