我是 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();
^~
原文由 Abhi38 发布,翻译遵循 CC BY-SA 4.0 许可协议
我在您的代码中没有发现任何错误。我在这里编译了您的代码( 在此处 复制您的代码并在编译前选择语言 C++17),它按预期工作。只需检查您正在运行的编译器是否支持 C++17 功能。因为 std::string_view 仅从 C++17 开始可用。要在 Visual Studio 中启用 C++17, 请在此处检查 并在 Visual Studio 代码中 遵循此。
希望这会帮助你。