我正在使用VS Code 来编写 C++ 代码。它表现得很好。但是每当我在代码中使用auto关键字时,程序就无法编译。
例如,要迭代字符串,我的代码不使用 auto 关键字将如下所示
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello");
for(int i=0;i<s.length();i++)
{
cout<<s.at(i)<<' ';
}
cin.get();
}
Run Code Online (Sandbox Code Playgroud)
它编译 find 并运行并给出正确的输出。
执行任务:g++ -g -o helloworld helloworld.cpp
终端将被任务重用,按任意键将其关闭。
输出:你好
但是每当我尝试执行相同的工作但使用 auto 关键字时,代码看起来像
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello");
for(auto c:s)
{
cout<<c<<' ';
}
cin.get();
}
Run Code Online (Sandbox Code Playgroud)
但它给出了编译时错误
Executing task: g++ -g -o helloworld …
Run Code Online (Sandbox Code Playgroud)