No function overload in C

Source: Internet
Author: User

After studying C/C ++ for so many years, I have been attacked by a kind of pitfalls! "Function overloading has always been the standard of C !"???????? Are you also deeply eroded by this idea? Do you think there is a function overload in the C language! Now I want to congratulate you and you have won the bid! There is no function overload in the Standard C language. [Cpp] the following code is in the extern "C" {void MyTestFun (int nArray) {printf ("MyTestFun \ n");} void MyTestFun (char * strArray) under the vs2010 Compiler) {printf ("MyTestFun: % s \ n", strArray) ;}// the compiler reports error C2733: second C linkage of overloaded function 'mytestfun' not allowed, moreover, "a function name can only have one definition, but can have multiple declarations. A function name can only have one definition, but can have multiple declarations, and call [cpp] such as: program A // In A stdafx. define extern "C" {void MyTestFun (int n) {printf ("MyTestFun: % d", n) ;}} in the cpp file );}}; // different declarations and calls in another cpp file, of course, must be consistent with the call (must be in the previous different cpp file) extern "C" void MyTestFun (int, int); int _ tmain (int argc, _ TCHAR * argv []) {MyTestFun (1, 2); return 0 ;}// output result: MyTestFun: 1 _ tmain function is a standard C function our _ tmain function is a standard C function, so our _ tmain function can be defined as the following forms: int _ tmain (void); int _ tma In (int argc) int _ tmain (int argc, _ TCHAR * argv []) int _ tmain (int argc, _ TCHAR * argv [], wchar_t * envp [])... [cpp] 1) in tchar. in the H file, define _ tmain macro: # define _ tmain main 2) in internal. in the H file, wmain extern "C "{... int _ CRTDECL wmain (_ In _ int _ Argc, _ In_count _ (_ Argc) _ Pre_z _ wchar_t ** _ Argv, _ In_z _ wchar_t ** _ Env );... _ declspec (noinline) int _ tmainCRTStartup (void ){... # ifdef WPRFLAG _ winitenv = envp; Mainret = wmain (argc, argv, envp); # else/* WPRFLAG */_ initenv = envp; mainret = main (argc, argv, envp ); # endif/* WPRFLAG */...}...} I have A problem, but I have A problem, that is, if the above program A becomes the following program B: [cpp] program B stdafx. void MyTestFun (int n) in cpp // is not defined as C function {printf ("MyTestFun: % d", n );} extern "C" void MyTestFun (int, int); int _ tmain (int argc, _ TCHAR * argv []) {MyTestFun (1, 2 ); return 0;} // This compilation fails, error LN K2019: unresolved external symbol _ MyTestFun referenced in function _ wmain is obvious, c and c ++ functions are compiled with the function name (this is also the implementation method of C ++ function overloading), but the methods are different, the name of the MyTestFun () function defined and declared is different after the compiler completes the compilation. Therefore, the compiler cannot find the definition of the MyTestFun () function, but _ tmain () the definition does not add extern "C", but this does not happen. Does the compiler perform special processing on _ tmain, is it possible to specify in the compiler that our function can also compile the function in the cpp file according to the c compilation method like _ tmain !??

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.