Thread: undefined reference to `WinMain@16'

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    46

    undefined reference to `WinMain@16'

    This code will compile, but will produce the above error when I try to run.
    Why is this?

    Code:
    class Test {	
    	public:
    		int main (int, char **);
    };	
    int Test::main (int argc, char *argv[]) {
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Why is this?
    Because you don't have a main function. Below the code you posted, you would need to add:
    Code:
    int main(int argc, char *argv[])
    {
       Test main(argc, argv);
    
       return 0;
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As it says, there is no main function in this code.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Thanks for the replies.

    Swoopy, I tried adding your code exactly below my code, but I get compiler errors:
    Code:
    no matching function for call to `Test::Test (int &, char **&)'
    But if I take my original code, and then remove the 'Test::main' and replace with just 'main' then it works.
    What is the difference between defining a function 'int Test::main()' and just 'int main()'?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Swoopy, I tried adding your code exactly below my code, but I get compiler errors:
    Oops, I goofed. It should be:
    Code:
    int main(int argc, char *argv[])
    {
       Test main;
       main.main(argc, argv);
    
       return 0;
    }
    >What is the difference between defining a function 'int Test::main()' and just 'int main()'?
    Test::main is just a member function named main inside of a class called Test. int main() is the function called to invoke any C++ program. Every program must have either a main (for a Console application) or a WinMain (for a windows application).

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Ah ok I think I got it.
    Although naming the Test object delared in main() as main was a little confusing.

  7. #7
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    It strikes me that C++'s OOP capabilities are really just a hack. Not as complete as say Java or .NET which are entirely based around OOP.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    That's mainly what sucks about them. You can't choose the approach you wish to take in your algorithms. If you want to go all procedural you're f***ed I guess.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Although naming the Test object delared in main() as main was a little confusing.
    True maybe this is better:
    Code:
    int main(int argc, char *argv[])
    {
       Test test1;
       test1.main(argc, argv);
    
       return 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could also make a constructor that takes argc and argv as arguments, which would automatically get called when a Test class is constructed.
    Code:
    #include <iostream>
    class Test {	
    	public:
    		Test(int, char **);
    };	
    Test::Test(int argc, char *argv[]) {
    	std::cout << "In Test constructor." << std::endl;
    }
    
    int main(int argc, char *argv[])
    {
       Test test1(argc, argv);
    
       return 0;
    }
    Last edited by swoopy; 09-29-2006 at 09:14 AM. Reason: Removed "return 0" from constructor

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Frobozz
    It strikes me that C++'s OOP capabilities are really just a hack. Not as complete as say Java or .NET which are entirely based around OOP.
    C++'s OO capabilities are just as complete, if not more so, as those of Java or C#. It's just that unlike these languages, C++ has other things to offer, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by CornedBee
    C++'s OO capabilities are just as complete, if not more so, as those of Java or C#. It's just that unlike these languages, C++ has other things to offer, too.
    Exactly!
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM