为什么 g++ 显示“gets()”未声明,即使在包含 <cstdio> 之后

Niv*_*R.S 0 c++ string gcc g++ geany

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    char str[30];
    gets(str);
}
Run Code Online (Sandbox Code Playgroud)

当我使用gets()函数编译器给我以下错误

error: 'gets' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

我在 geany ide 中使用 G++

请使解决方案简单,因为我是初学者。

Nat*_*ica 5

gets在 C++11 中被弃用并从 C++14 中删除。如果您使用 GCC6.0 或更新版本,那么默认情况下它使用 C++14 并且将不可用。而不是使用

main()
{
    char str[30];
    gets(str);
}
Run Code Online (Sandbox Code Playgroud)

int main()
{
    std::string str;
    std::getline(cin, str);
}
Run Code Online (Sandbox Code Playgroud)