Brian Overland wrote:And delcare main either "int" or "void."
... main must be declared either "int" or "void". If you don't want to return a value, declare it void!
An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined. All implementations shall
allow both of the following definitions of main:
and
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Find the latest job vacancies from the top employers here at QuickList.
We are the prime job site for Filipinos.
Brian Overland wrote:Hey, there should be no semi-colon after "main"!! And declare main either "int" or "void." (See examples below.)
Hopefully my book never ever ever commits this error... I checked the code by compiling it myself many times.
BUT... I feel for you if you are a new programmer! Unlike the various forms of Basic, C++ does use semi-colons and you have to know where to put them. (Hey, no offensive joke replies to that, please. Be nice.) You put semi-colons at the ends of statements.
"main" is the name of a function. It is not, itself, a statement. However, you can have statements inside main -- you can even (technically legal though not terribly useful in most cases) have an empty statement by placing a semi-colon inside of main(). Here is a version of main that has one statement that does nothing.
OH, and by the way(!)... main must be declared either "int" or "void". If you don't want to return a value, declare it void! So the following compiles on most C++ compilers:
void main() {
;
}
Increasingly, however, it is considered more "standard" to always declare main() with a return value. So you would write this:
int main() {
return 0;
}
Why return 0? Often it does not matter, but the return value of main is a code to the operating system that indicates success or failure. 0 means success.
Hope this helps,
Brian Overland
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|