错误:命名空间“std”中没有名为“async”的成员

error: no member named 'async' in namespace 'std'

提问人:coda 提问时间:11/21/2020 最后编辑:Florian Weimercoda 更新时间:11/21/2020 访问量:2245

问:

我知道这是C++11的事情,但我非常确定我的编译器支持C++11。std::async

#include <future>
using namespace::std;

void functionToRun() {
  // some code
}

int main() {
    auto x = 2; // throws warning warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
    std::future<void> metricLoggerFuture = std::async(std::launch::async, functionToRun); // throws error  no member named 'async' in namespace 'std'
}

如果我使用

std::future<void> metricLoggerFuture = async(std::launch::async, functionToRun); // error: use of undeclared identifier 'async'

g++ --version 也显示

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

我经历了以下几点,

  • 操作系统: macOS Catalina
  • 这段代码是一个更大的项目(TigerVNC 上的包装器)的一部分,所以有一个 makefile,但由于符合没有任何问题,我认为 C++11 不是问题,因此传入 和 也无济于事。auto-std=c++11CXXFLAGSCPPFLAGS
macOS +11 Clang ++ STDASYNC 标准未来

评论

11赞 NathanOliver 11/21/2020
引发警告警告:“auto”类型说明符是 C++11 扩展 [-Wc++11-extensions] 表示您未使用 C++11。您的 makefile 或编译器命令中的某些内容不正确。你能发布这些信息吗?
0赞 coda 11/21/2020
谢谢内森,我在 makefile 中的一个地方失踪了。如果您单独发布,我可以将您的答案标记为“答案”:)std=c++11

答:

1赞 Fantastic Mr Fox 11/21/2020 #1

看起来您没有使用 、add(或更高版本)编译到编译器命令行或 makefile。c++11-std=c++11