我正在尝试将 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 经验来自行诊断这个问题。我进行了强制性的谷歌搜索,但没有结果。
感谢您的帮助。
最佳答案
看起来实际错误在于<boost/throw_exception.hpp>
之前的 header 中.例如。你的翻译单元包含类似
#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?)
。 ,你通常需要检查发生错误的标题之前的标题。
最后,您可以通过在每个翻译单元中使用标准化的 header 包含顺序来消除此问题。我通常使用这个顺序:
- C 标准库
- C++ 标准库
- C 第三方库
- C++ 第三方库
- 我的标题
如果您使用此命令,则该错误将显示在您自己的代码中(如果存在),而不是出现在您无法控制的某些第三方 header 的内部。
关于c++ - Boost 中的编译错误 throw_exception.hpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864862/