C++ 6031:在visual studio 2019中忽略了返回值(getchar()
它不会影响我的代码,但在更新VisualStudio之前,我从未见过这样的问题。我不知道这是否有关联,但我很困惑为什么会有问题C++ 6031:在visual studio 2019中忽略了返回值(getchar(),c++,visual-studio-2019,C++,Visual Studio 2019,它不会影响我的代码,但在更新VisualStudio之前,我从未见过这样的问题。我不知道这是否有关联,但我很困惑为什么会有问题 #include <iostream> #include <string> #include <array> using namespace std; int main() { const int SIZE = 3; array<string, SIZE> names = { "S","A","W"
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main() {
const int SIZE = 3;
array<string, SIZE> names = { "S","A","W" };
array<string, SIZE>::iterator it;
cout << "names: \n";
for (it = names.begin(); it != names.end(); it++)
cout << *it << endl;
getchar();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
常数int SIZE=3;
数组名={“S”、“A”、“W”};
数组::迭代器;
cout当visual studio更新时,他们在getchar
中添加了一个[[nodiscard]]
属性。这告诉编译器在忽略函数返回值时警告用户。您可以在此处找到更多信息:
在这种情况下,由于使用getchar
只是为了防止窗口关闭,因此不需要返回值,因此可以忽略此警告
我们可以通过显式忽略返回值使警告静音:
(void)getchar(); //Explicitly ignore return value
在这种情况下,我个人的解决方案是在控制台中暂停,将其加倍,如下所示:
getchar();getchar();我本打算建议#pragma警告(suppress:6301)
,但我更喜欢您的解决方案。它胜过了a)配置MSV“自动关闭控制台”设置,b)#pragma suppress,或c)忽略愚蠢的警告。它还有一个额外的好处,即可移植,并提供有意义的“意向声明”。很好的建议-很好!解决方案是正确的,但是[[nodiscard]]
将是编译器诊断C4834。C6000以上是代码分析诊断。这一个对应于SAL注释。从2020.06.03起,我正在尝试(void)getchar();而且它不起作用。我个人在这种情况下的解决办法是像getchar();getchar()一样将它加倍;