【问题标题】:error: 'string_view' is not a member of 'std'错误:“string_view”不是“std”的成员
【发布时间】:2020-02-02 09:49:55
【问题描述】:

我是 C++ 新手。我在windows10的visual-studio-code中编译了我的代码,有2个类型为string和string_view的变量。字符串变量很好,但是 string_view 给出了错误。我还在 configuration.json 中启用了 c++17 扩展,并在 vscode 中编辑 configuration/ui 文件。

这是我的代码:=

#include<iostream>
#include<string_view>
using namespace std;
int main(){
      string str="hello";
      cout<<str<<endl;
      std::string_view sv=" world";
      auto result=str+sv.data();

      return 0;
}

错误是:=

main.cpp: In function 'int main()':
main.cpp:7:12: error: 'string_view' is not a member of 'std'
       std::string_view sv=" world";
            ^~~~~~~~~~~
main.cpp:7:12: note: 'std::string_view' is only available from C++17 onwards
main.cpp:8:23: error: 'sv' was not declared in this scope
       auto result=str+sv.data();
                       ^~

【问题讨论】:

  • 你用的是什么编译器? Visual Studio Code 是一个文本编辑器。它不是用于编译和构建的 IDE。
  • 我正在使用 g++。

标签: c++ visual-studio-code c++17


【解决方案1】:

我通过去..解决了这个错误 项目>属性...选择所有配置...> 配置属性 > C/C++ > 语言 > C++ 语言标准 > ...选择 ISO C++ 17 标准(/std::c++17)enter image description here

【讨论】:

    【解决方案2】:

    默认情况下,VS 将项目设置为 x86,但是当你打开 属性,它会加载 x64。我认为VS应该加载相同的 您当前在 IDE 中使用的设置,但这只是 个人意见。

    为 x86 设置 C++17 标准解决了我的问题。 这个解决方案对我有用 https://developercommunity.visualstudio.com/content/problem/1133349/namespace-std-has-no-member-string-view.html

    【讨论】:

      【解决方案3】:

      我正在为 Visual Studio 代码使用 gcc 编译器。在 C++17 中,gcc 进行了一些更改,包括将 string_view 和 string 集成在一起。 我认为现在看起来更复杂了,因为在某些情况下,字符串会隐式转换为 string_view。

      更多详情:

      http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0254r2.pdf

      【讨论】:

        【解决方案4】:

        我没有在您的代码中发现任何错误。我编译了您的代码here(在此处复制您的代码并在编译前选择语言 C++17)并且它按预期工作。只需检查您正在运行的编译器是否支持 C++17 功能。因为 std::string_view 仅从 C++17 开始可用。 在 Visual Studio check here 和 Visual Studio 代码 follow this 中启用 C++17。

        希望这会对你有所帮助。

        【讨论】:

        • @Abhi38 我已经在上面的答案中给了你链接。请调查一下。谢谢
        • 感谢您的回复。但是,我使用的是视觉工作室代码而不是视觉工作室17
        • @Abhi38:Visual Studio Code 不是您的编译器。您的代码如何编译由您的编译器控制。所以这就是你需要专注于启用你正在编译的语言。
        • @Abhi38 我希望你现在明白了。谢谢
        • @Abhi38 你能正确地纠正我的回答吗?这对其他人也将非常有用。谢谢。
        猜你喜欢
        • 2014-11-23
        • 2014-08-27
        • 2021-09-07
        • 2013-08-07
        • 2017-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多