C++ 错误:“::main”必须返回“int”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40381960/
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-08-28 15:23:10  来源:igfitidea点击:

ERROR: '::main' must return 'int'

c++

提问by SPLASH

This my main function:

这是我的主要功能:

void main(int argc, char **argv)
{
    if (argc >= 4)
    {
        ProcessScheduler *processScheduler;
        std::cout <<
            "Running algorithm: " << argv[2] <<
            "\nWith a CSP of: " << argv[3] <<
            "\nFilename: " << argv[1] <<
            std::endl << std::endl;

        if (argc == 4)
        {
            processScheduler = new ProcessScheduler(
                argv[2],
                atoi(argv[3])
            );
        }
        else
        {
            processScheduler = new ProcessScheduler(
                argv[2],
                atoi(argv[3]),
                atoi(argv[4]),
                atoi(argv[5])
            );
        }
        processScheduler -> LoadFile(argv[1]);
        processScheduler -> RunProcesses();

        GanntChart ganntChart(*processScheduler);
        ganntChart.DisplayChart();
        ganntChart.DisplayTable();
        ganntChart.DisplaySummary();

        system("pause");

        delete processScheduler;
    }
    else
    {
        PrintUsage();
    }
}

The error I get when I compile is this:

我编译时得到的错误是这样的:

Application.cpp:41:32: error: '::main' must return 'int'

Application.cpp:41:32: 错误:'::main' 必须返回 'int'

It's a void function how can I return int and how do I fix it?

这是一个 void 函数,我如何返回 int 以及如何修复它?

回答by Michael

Try doing this:

尝试这样做:

int main(int argc, char **argv)
{
    // Code goes here

    return 0;
}

The return 0;returns a 0 to the operating system which means that the program executed successfully.

所述return 0;返回一个0到操作系统,这意味着程序成功执行。

回答by Nick Pavini

C++ requires main()to be of type int.

C++ 要求main()类型为int.

回答by renonsz

Function is declared as int main(..);, so change your voidreturn value to int, and return 0at the end of the main function.

函数被声明为int main(..);,因此将void返回值更改为int,并return 0在主函数的末尾。