Multi-definition Link error caused by special C ++ function templates

Source: Internet
Author: User

The title may not be very clear. To explain it, the function templates are generally placed in the header file. So sometimes, I will also make a special one and put it in this header file, when this header file is included multiple times, multiple definitions of links may occur. For example:

Symptom description

File "header. H"

#ifndef HEADER#define HEADERtemplate <class T>size_t size_rb_tree_node(){    return 20; /*constant value for l r p pointer and (color & height) and void * value*/}template <>size_t size_rb_tree_node<void *>(){    return 30;}#endif

"Source. cpp"

#include "Header.h"int count(){    int a = size_rb_tree_node<int>();    return a;}

"Main. cpp"

#include "Header.h"using namespace std;int count();int main(){    int a = size_rb_tree_node<int>();    a = size_rb_tree_node<void *>();}

After compilation, an error is reported during the link:

1>------ Build started: Project: Win32Project1, Configuration: Debug Win32 ------1>  AllocatorNew.cpp1>Source.obj : error LNK2005: "unsigned int __cdecl size_rb_tree_node<void *>(void)" (??$size_rb_tree_node@PAX@@YAIXZ) already defined in AllocatorNew.obj1>C:\Users\tianzuoz\Documents\Visual Studio 2012\Projects\Win32Project1\Debug\Win32Project1.exe : fatal error LNK1169: one or more multiply defined symbols found========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

In fact, the reason is very simple, because the special function is a common function, which defines a function in a header file, and then multiple definitions will be caused by the same results as the include header file.

 

Solution

1. Add the inline mark to the special function. In this way, the compiler will not generate a function symbol for this function, so it will be considered as a macro. However, some compilers may not necessarily be inline. If you can't try it, you can see the following:

#ifndef HEADER#define HEADERtemplate <class T>size_t size_rb_tree_node(){    return 20; /*constant value for l r p pointer and (color & height) and void * value*/}template <>inline size_t size_rb_tree_node<void *>(){    return 30;}#endif

2. It is also possible to make this function a file domain that does not participate in global link:

#ifndef HEADER#define HEADERtemplate <class T>size_t size_rb_tree_node(){    return 20; /*constant value for l r p pointer and (color & height) and void * value*/}template <>static size_t size_rb_tree_node<void *>(){    return 30;}#endif

3. Another way is to take this special object from the original file, put it in the required implementation file, and then add the static attribute.

 

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.