Thread: Help.....can't even get started. "Multiple definitions of 'main' " error

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    1

    Unhappy Help.....can't even get started. "Multiple definitions of 'main' " error

    Im trying to learn C++ for my next year at Uni, started using codeblocks to write in C and so was happy to keep using it for C++, but Im having an issue that I can't seem to find an answer to.

    I bought the book, "Jumping into C++" and followed the instructions, opened up a new project and basically started from scratch again. Hello world works great! However when I build the second program, it comes up with an error telling me that there are multiple definitions of main, and that it's first defined in line 5 of hello.cpp

    Ive tried renaming files but I havent got a clue what Im doing and I have no idea how to fix this......

    hello.cpp
    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {
        cout << "Hello World: \n";
    }
    readnum.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    
    
    {
        int number;
    
    
        cout << "Please enter a number: ";
        cin >> number;
        cout << "You entered: " << number << "\n";
    }
    Please help...

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you cannot have two functions named main in your project. it has nothing to do with file names.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    The following explanation is probably an oversimplification and not 100% correct, but it will do to get the point across. When you compile and link your program, the linker must figure out where control flow begins in your program (i.e. where to start running). It does that by looking for a function named main. Acceptable function signatures (to my knowledge) are:

    int main();

    and

    int main( int argc, char** argv );

    Anyway, if your program has multiple functions named main, the linker won't know which one is supposed to be your program entry point, so it just quits and dies.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is perhaps a good practical way of looking at why it's not allowed (there could be other reasons, though).
    A pedantic answer is: it doesn't work simply because the standard says it is not allowed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    as I understand it, main is not required to have any particular signature apart from its name and return type. section 3.6.1.2 of the standard says that the signature of main is implementation-defined. this is why windows and apple are allowed to add extra parameters. it must be called main, and must return an int, but the standard does not specify what arguments it takes, with the following exceptions: it specifies that all implementations must allow int main(void) and int main(int argc, char** argv).
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Elkvis View Post
    as I understand it, main is not required to have any particular signature apart from its name and return type. section 3.6.1.2 of the standard says that the signature of main is implementation-defined. this is why windows and apple are allowed to add extra parameters. it must be called main, and must return an int, but the standard does not specify what arguments it takes, with the following exceptions: it specifies that all implementations must allow int main(void) and int main(int argc, char** argv).

    Yeah, you're right:


    3.6.1 Main function
    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:

    Code:
    int main() { /* ... */ }
    and

    Code:
    int main(int argc, char* argv[]) { /* ... */ }
    Well, I did say my explanation likely wasn't going to be 100% correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-18-2011, 11:39 AM
  2. 5 times "local function definitions are illegal"
    By Borislav in forum C++ Programming
    Replies: 24
    Last Post: 01-11-2011, 02:37 AM
  3. Help! Linker can't find definitions in ".a" library!
    By 6tr6tr in forum Windows Programming
    Replies: 19
    Last Post: 04-18-2008, 03:56 PM
  4. Getting a "multiple definition of <classname>" error.
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2007, 07:43 PM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM