C ++ |主要功能错误|初学者 [英] C++ | main function error | beginners

查看:109
本文介绍了C ++ |主要功能错误|初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对c ++完全陌生,并且正在使用eclipse.

I'm totally new with c++ and I'm using eclipse.

但是...我不知道为什么在主函数中会出现此错误:

but... I don't know why I get this error at the main function:

错误::: main必须返回int

ERROR: ::main must return int

我的代码是:

void main()
{
char a;
while (a!='q')
{
    string ln = "enter option: ";
    cout<< ln;

    switch(a)
    {
    case 1:
        if (a=='1')
            func1();
        break;
    case 2:
        if (a=='2')
            break;
        break;
    }
}
}

推荐答案

由于在C ++中,main函数的返回类型必须为int.

Because in C++, the main function must have a return type of int.

您的返回类型为void的版本不正确,并且已被编译器正确拒绝.

Your version with a return type of void is incorrect and is being correctly rejected by your compiler.

只需更改声明即可

void main()

int main()

还有另一种形式,它允许您处理在命令行上传递给程序的参数.看起来像这样:

There is an alternative form that allows you to process arguments passed on the command line to your program. It looks like this:

int main (int argc, char *argv[])

但是,当您只是学习C ++并尝试在屏幕上打印"hello world"时,您可能不必担心这一点.您最终会到达那里.

but when you're just learning C++ and trying to print "hello world" on the screen, this is probably not something you need to worry about. You'll get there eventually.

并考虑更新您用来学习C ++的书.如果它使入口点的功能签名错误,那么其他更复杂的事情也可能会出错?!第一次学习错误的语言毫无意义.可以在此处获得建议书的列表.

And consider updating the book you're using to learn C++, too. If it's getting the function signature of the entry point wrong, what other more complicated things might it also be getting wrong?! No point in learning the language wrong the first time around. A list of suggested books is available here.

这篇关于C ++ |主要功能错误|初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆