简体   繁体   中英

Message "error: use of undeclared identifier 'assert'"

I had a piece of code that was including some Boost headers. Upon compilation I received errors like

/usr/local/include/boost/smart_ptr/shared_ptr.hpp:1041:9: error: use of undeclared identifier 'assert'

        BOOST_ASSERT( deleter_.use_count() <= 1 );

        ^

/usr/local/include/boost/assert.hpp:60:29: note: expanded from macro 'BOOST_ASSERT'

# define BOOST_ASSERT(expr) assert(expr)
                                ^

These errors however only occurred on Windows and macOS.

Explicitly including either <cassert> or <assert.h> before the Boost headers had no effect.

You need to #include <cassert> to bring in the assert implementation.

It's your job to define or not define NDEBUG accordingly.

I'm surprised Boost doesn't do that for you - are you using the Boost files correctly (ie including the files that you're supposed to)?

As it turned out I had a file called Assert.h in my include-path (a custom file of mine). On case-insensitive file systems as used by Windows and macOS, this would shadow the original assert.h header that actually defines the assert macro.

The solution therefore was simply to rename my assert-header file.

(I found the solution thanks to [Compilation Error] error: use of undeclared identifier 'assert' #15 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM