简体   繁体   English

消息“错误:使用未声明的标识符‘断言’”

[英]Message "error: use of undeclared identifier 'assert'"

I had a piece of code that was including some Boost headers.我有一段代码,其中包含一些 Boost 标头。 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.然而,这些错误只发生在 Windows 和 macOS 上。

Explicitly including either <cassert> or <assert.h> before the Boost headers had no effect.在 Boost 头文件之前显式包含<cassert><assert.h>无效。

You need to #include <cassert> to bring in the assert implementation.您需要#include <cassert>来引入assert实现。

It's your job to define or not define NDEBUG accordingly.相应地定义或不定义NDEBUG是您的工作。

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)?我很惊讶 Boost 并没有为您做到这一点 - 您是否正确使用了 Boost 文件(即包括您应该使用的文件)?

As it turned out I had a file called Assert.h in my include-path (a custom file of mine).事实证明,我的包含路径中有一个名为Assert.h的文件(我的自定义文件)。 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.在 Windows 和 macOS 使用的不区分大小写的文件系统上,这将assert.h实际定义assert宏的原始assert.h标头。

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 .) (由于[Compilation Error] error: use of undeclared identifier 'assert' #15 ,我找到了解决方案。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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