1

I am trying to use Boost's lexical_cast for my C++ project but am running into compilation errors using Visual Studio 2010 Professional.

Error is as follows:

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.

and here is the code that is using lexical_cast (it is not related, but who knows it may help)

#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;
}

Unfortunately I do not have enough experience with Boost to diagnose this problem on my own. I did the obligatory google-ing with no results.

Thank you for any help.

ssell
  • 6,429
  • 2
  • 34
  • 49

2 Answers2

7

It looks like the actual error lies in the header before <boost/throw_exception.hpp>. E.g. your translation unit contains something like

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

//You translation-unit specific code in here

where "myheader.hpp" contains something like

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

Generally speaking, if you can't even compile a header file, and you get a error C2447: '{' : missing function header (old-style formal list?), you usually need to check the header before the one where the error occurs.

Finally, you can eliminate this problem by using a standardized header include order inside each translation unit. I usually use this order:

  • C Standard Libraries
  • C++ Standard Libraries
  • C Third Party Libraries
  • C++ Third Party Libraries
  • My Headers

If you use this order, the bug will show up in your own code if it exists, rather than in the bowels of some third party header you have no control over.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Is there some rationale in including standard C libraries before C++ ones, or is just for neatness? – Emile Cormier May 03 '11 at 15:23
  • Nevermind, found this question (http://stackoverflow.com/questions/2762568/c-c-include-file-order-best-practices) that discusses include ordering. – Emile Cormier May 03 '11 at 15:26
  • The issue was indeed header inclusion ordering. Forgot to accept this answer when I discovered the issue. – ssell Jul 12 '11 at 20:38
  • So what order made it work? And why is this the accepted answer if this isn't the solution? – paulm Jan 22 '15 at 11:38
  • @paulm: Changing the order doesn't make it work. Fixing the missing semicolon or } or similar makes it work. Its likely the accepted answer because it helped the OP find the actual mistake in his / her code. – Billy ONeal Jan 22 '15 at 19:53
0

I had the exact same error because some one decided it would be cool to create a macro called throw_exception.

This then caused a build failure if the header containing this #define was ever included before the boost header. This is because:

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

Would have "throw_exception" replaced with the macro creating invalid code.

paulm
  • 5,629
  • 7
  • 47
  • 70