error with 'void main()' in C++ tool

Hey there. I've just started taking a object-oriented C++ course online -- it's been a few years since I've programmed C++, although I've programmed C more recently. Anyways, they expect me to use Visual Studio as my compiler, but I'm not budging -- Xcode is where I like to write my code, and I don't feel like reinstalling Parallels. The problem is, the code I'm supposed to use for one of the exercises defines main() using 'void' instead of 'int'. In my experience programming C (and C++ if I'm not mistaking), this has never been a problem. However, with Xcode I'm getting an error that says, "error: '::main' must return 'int'". Can anyone toss me some insight into why I'm getting this error, and how I may be able to correct it so that the compiler accepts main() as 'void'? I've checked the project settings under "GCC 4.0 - Language", but I didn't find anything I could recognize as relevant, other than "C Language Dialect" being set to C99, and I don't think that's the issue.

Any help is appreciated -- thanks.

MacBook, Mac OS X (10.5.8)

Posted on Sep 5, 2010 4:21 AM

Reply
11 replies

Sep 6, 2010 6:24 AM in response to Tron55555

Tron55555 wrote:
So I guess there's no easy way around that then?


The easy answer is to use int. Like a number of things in the Windows world, "void main()" is flat-out syntactically invalid. The Microsoft Foundation Class library will not compile with a standards-compliant C++ compiler - because it isn't valid C++. Microsoft's compilers have switches to enable standards compliance, but you should never use them. GCC has switches to enable it to compile MFC, but no one ever uses them.

Apple gets a lot of press with the iPad and iPhone, but the reality is that Macs are still a distant minority in any corporate/educational environment. University professors, including those in Computer Science, are known for living 20 years in the past.

You are going to have to do what every Mac user in a computer science class has always done, write your code on your Mac because you love the Mac, then port it to Windows or whatever else. You are going to need Parallels for this course. The end result is that your code will be better than anyone else's in the class. You have to write code good enough to satisfy two compilers and you will be a better programmer for it.

Sep 6, 2010 5:12 AM in response to etresoft

Glad to see some familiar faces around here -- been awhile.

Anyways, the problem is that the instructor expects me to use 'void main()' and not 'int main()'. Normally I obviously wouldn't think making a small change like that would be a problem, at least not for a basic exercise like the one we're doing, but I've had some pretty quirky profs in the past and I didn't want to take any chances, it still being the beginning of the semester and all.

Since GCC doesn't allow for 'void main()', and since I don't feel like installing Parallels and then Windows all for the sake of Visual Studio (nor do I have the hard drive space to partition my drive at the moment), I ended up simply using 'int main()' and explaining via comment that my compiler didn't allow for main() to be defined as 'void'. So hopefully it won't be an issue, but I thought I'd come here to see if there was some setting that would force the compiler to allow me to use 'void main()' so that I wouldn't have to acknowledge to the professor that I'm using a Mac, since technically I think I'm supposed to be on a PC for this course.

So I guess there's no easy way around that then? Not really a big deal, just thought I'd try. Thanks for the replies.

Sep 8, 2010 4:21 AM in response to etresoft

Thanks, et. Good information.

Out of curiosity, you mentioned that "void main()" is technically invalid C++ code -- would that apply to C as well? The reason I ask is because, in .c file in Xcode, "void main()" only raises a warning, as opposed to the actual error raised in a .cpp file. Is "void main()" invalid in standard C as well? I would assume so, given that C++ is a superset of C, but just curious...

Thanks for the replies.

Sep 8, 2010 7:01 AM in response to Tron55555

Tron55555 wrote:
Out of curiosity, you mentioned that "void main()" is technically invalid C++ code -- would that apply to C as well? The reason I ask is because, in .c file in Xcode, "void main()" only raises a warning, as opposed to the actual error raised in a .cpp file. Is "void main()" invalid in standard C as well? I would assume so, given that C++ is a superset of C, but just curious...


It depends. You should always have a result value for main, but C89 doesn't require it. C++ is much closer to the C99 standard, where void main() is illegal.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

error with 'void main()' in C++ tool

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.