visual-studio 如何处理 Visual Studio 2008 中的警告 C4100

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2519851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:41:53  来源:igfitidea点击:

How to deal with Warning C4100 in Visual Studio 2008

c++visual-studio

提问by Jimmie

For some reason my Visual Studio 2008 began to show warnings for code like: "int main( int argc, char **argv)", which is really annoying.

出于某种原因,我的 Visual Studio 2008 开始显示代码警告,例如:“int main(int argc, char **argv)”,这真的很烦人。

The detailed warning ouputs are (you can ignore the line numbers):
1>.\main.cpp(86) : warning C4100: 'argv' : unreferenced formal parameter
1>.\main.cpp(86) : warning C4100: 'argc' : unreferenced formal parameter

详细的警告输出是(您可以忽略行号):
1>.\main.cpp(86) : warning C4100: 'argv' : unreferencedformal parameter
1>.\main.cpp(86) : warning C4100: ' argc' : 未引用的形式参数

I wonder if there are settings in Visual Studio 2008 that have been accidentally changed. Or how should I deal with this warning?

我想知道Visual Studio 2008 中是否有不小心更改的设置。或者我应该如何处理这个警告?

回答by James McNellis

If the parameters are unreferenced, you can leave them unnamed:

如果参数未引用,您可以不命名它们:

int main(int, char**)
{
}

instead of

代替

int main(int argc, char** argv)
{
}

If you really want just to suppress the warning, you can do so using the /wd4100command line option to the compiler or using #pragma warning(disable: 4100)in your code.

如果您真的只想取消警告,您可以使用/wd4100编译器的命令行选项或#pragma warning(disable: 4100)在您的代码中使用。

This is a level 4 warning; if you compile at a lower warning level, you won't get this warning. The warning level is set in the project properties (right-click project, select Properties; on the Configuration Properties -> C++ -> General, set "Warning Level").

这是 4 级警告;如果您在较低的警告级别进行编译,您将不会收到此警告。警告级别在项目属性中设置(右键项目,选择属性;在Configuration Properties -> C++ -> General,设置“Warning Level”)。

回答by CB Bailey

If you're not using the command line parameters then the other standard signature for main is:

如果您不使用命令行参数,那么 main 的另一个标准签名是:

int main();

回答by Michael Burr

Warning C4100 is issued at warning level 4 which is not default, so at some point someone probably changed it for your project.

警告 C4100 在警告级别 4 发出,这不是默认值,因此在某些时候有人可能为您的项目更改了它。

You could change the warning level back, or address the warning more directly.

您可以更改警告级别,或者更直接地解决警告。

As James McNellis said, you can silence the warning in C++ by removing the parameter name from the parameter list. However, if the code will be compiled as C code you'll get an error in that case.

正如James McNellis 所说,您可以通过从参数列表中删除参数名称来消除 C++ 中的警告。但是,如果代码将被编译为 C 代码,在这种情况下您将收到错误消息。

The Windows headers define the macro UNREFERENCED_PARAMETER()to help deal with this warning. You could use

Windows 标头定义了UNREFERENCED_PARAMETER()帮助处理此警告的宏。你可以用

UNREFERENCED_PARAMETER( argc);
UNREFERENCED_PARAMETER( argv);

to silence the warning. If you don't want to include windows headers, the macro simply expands to a use of the parameter name in a do nothing expression:

使警告静音。如果您不想包含 Windows 标题,则宏只是扩展为在无操作表达式中使用参数名称:

#define UNREFERENCED_PARAMETER(P)          \
    /*lint -save -e527 -e530 */ \
    { \
        (P) = (P); \
    } \
    /*lint -restore */