我是C++的新手。我在Windows 10中的Visual Studio Code中编译了自己的代码,其中包含两个类型为string和string_view的变量。字符串变量没有问题,但是string_view则出现错误。我还在configuration.json中启用了C++17扩展,并在VSCode的配置/ 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();
^~