Supplement to already defined in. OBJ solution for C ++ classic errors

Source: Internet
Author: User
23:00:16 label: already
Defined in. OBJ; C ++; link2005; template function; original work of template-type leisure workplace; allowed to be reproduced; During reprinting, please be sure to mark the original source, author information, and this statement in the form of hyperlinks. Otherwise, legal liability will be held. Http://jazka.blog.51cto.com/809003/643462 recently compiled an INI file operation class in the project, during the process of being referenced by other code, there will always be a link error, that is, error
Lnk2005: already defined in. OBJ. Remember this error also occurred when I first wrote the C ++ code. After the network information, the problem was solved smoothly, but this time the problem was somewhat different from the previous one, this is not a typical type of errors. Later I tried to solve this problem. First, we will introduce typical situations and solutions for this error. The following content is taken from the Web site: http://hi.baidu.com/3512/blog/item/d568bc340c002cb4d0a2d3d6.html. The program is in the following shape: a header file. H, a main function. cpp, and an operation function. cpp. The operation function contains the header file, and the main function contains the operation function. cpp. This error occurs during running. Solution 1: Change the operation function. cpp included in the main function to the header file. h, and then the above error can be solved. Solution 2: re-open the compiler, re-open the file, first open the main function. cpp, then compile and run, open FileView, where there is an external
Dependencies: add the header file. h and the operation function. cpp here. then compile and run the main function. cpp. The program runs successfully! Next, let's talk about the code that I encountered this error. This INI operation class contains two template functions, so all the member functions are put in the header file. H. When referenced, you only need to include this header file, that is, # include
"***. H", but when this operation is performed in multiple places, the above error will occur. In essence, my situation is also consistent with the above, but the form has changed, so the solution described above is not comprehensive enough. I would like to add it here. Solution:

Add the corresponding CPP file and add the header file. all the non-template functions in H are moved to the CPP file, and the implementation of the template functions are put into the file. h, so that when the header file is referenced in multiple places, the error will not occur again.

This article is from the "snow to sword" blog, please be sure to keep this source http://jazka.blog.51cto.com/809003/643462

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.