【问题标题】:namespace std:: does not contain optional命名空间 std:: 不包含可选
【发布时间】:2020-10-03 18:59:06
【问题描述】:

我正在做 Vulkan 教程 https://vulkan-tutorial.com/

#define GLFW_INCLUE_VULKAN
#include<GLFW/glfw3.h>
#include<optional>

struct s {
    std::optional<uint32_t> num;//Intellisense Error
};

int main() {
    return 5;
}

我从一个空项目开始,添加了包含和库; 我可以在不包含 std::optional 的情况下编译和运行。

当我使用 std::optional 时,我得到 c2039 "optional is not a member of std"

我正在运行 Windows 10 和 VisualStudio 2019

这是怎么回事?

谢谢。

【问题讨论】:

  • 您是否使用 C++17 支持进行编译?这种类型是在 C++17 中添加的。许多编译器仍然默认使用 C++14。
  • 您使用的是哪个 C++ 标准?因为std::optional 是 C++17 的特性。看这个帖子Change C++ Standard VS
  • 修复了它 THX。

标签: c++ stdoptional


【解决方案1】:

std::optional 需要 C++17。

直播Godbolt

您可以在MSVC 上使用/std:c++17 标志,在gcc/clang 上使用-std=c++17

【讨论】:

  • 在 Visual Studio 中需要添加标志 /std:c++17 到 Debug -> Project Properties -> C/C++ -> All options -> Additional Options
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多