【问题标题】:Use of undeclared identifier 'nothrow'; did you mean 'throw'? memory使用未声明的标识符“nothrow”;您指的是 'throw' 吗?记忆
【发布时间】:2017-08-15 02:31:38
【问题描述】:

我正在编写一个 cpp 代码以将一个应用程序从 Windows 移植到 Mac。在 Xcode 中构建应用程序时,它会抛出错误:

"Use of undeclared identifier 'nothrow'; did you mean 'throw'? memory"

这些错误在 cpp 标准库头文件中抛出。

以下是错误堆栈说明:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1/memory:83:8: Use of undeclared identifier 'nothrow'; did you mean 'throw'?

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1/string:48:10: In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1/string:48:

我在互联网上进行了搜索,但无法找到解决此问题的方法。任何建议都会有所帮助。为什么系统头文件会报错?

系统详情:

  • SDK 是 OSX 10.10。

  • 用于编译应用程序的编译器选项是 C++ 标准库:

    libc++(支持 C++11 的 LLVM C++ 标准。C++ 语言方言:GNU++11。C++ 编译器:Apple LLVM 6.0

【问题讨论】:

  • 我最好的猜测是你需要指定你正在使用的 C++ 标准
  • C++11 支持是我使用的标准
  • 感谢 Praveen Kumar。
  • 为了确保你真的在使用 C++11 检查(并显示)你的编译器 CLI 选项
  • 你好 zaufi。编译器使用的是 C++11。

标签: c++ xcode memory libstdc++ nothrow


【解决方案1】:

函数的 throw() 规范在 '11 标准中已被弃用,并在 '17 标准中被删除。如果 clang 不支持它,我的猜测是这是开发人员的有意选择,或者您正在使用 c++17 模式进行编译。

在现代 c++ 中正确的做法是使用 noexcept 规范。 noexcept 允许更高效的代码生成,因为它不必对抛出的异常执行 RTTI,而是如果从 noexcept 声明的函数下的调用帧抛出异常,则调用 std::terminate,short-循环由 '98 标准指定的疯狂的 std::unexpected() 机制。

【讨论】:

  • 谢谢。该代码仅在 C++11 中编译。为什么错误出现在 C++ 库内存文件中。同时下面提到的错误也来了。 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/locale:446:13:在语言环境文件中使用未声明的标识符“__throw_bad_alloc”。
【解决方案2】:

就我而言,这个问题只发生在调试模式下。 作为追查问题的结果,在头文件中声明如下代码时也出现了同样的错误。类文件中声明的代码没有引起任何问题。 通过删除头文件中声明的代码解决了问题。

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

希望这可以帮助仍在努力解决此问题的人...

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多