Friend function redefined / multiple definition

  It declares the friend function in a class header file, to achieve this function, link in the header file outside of class time there have been multiple definition

  Solution: The implementation declaration of a friend function as inline    or    in cpp in the achieved friend functions

 

Transfer: https://www.cnblogs.com/fnlingnzb-learner/p/5890065.html 

In a recent project, because I think == operator overloading structure, however, the effect of I overloaded in the process of writing a header file, resulting in a multiple definition of error. Now under summary Solution:

First of all, the most critical, not to define global variables and global approach in header files! ! ! !

1. Each header file to add conditional compilation:
Note: This method is not the way to solve the problem, but a method of solving multiple definition of.
When a plurality of files containing the same header file, and the file header without adding the condition compiled, interpreted independently, and then generates a separate file is generated for each identifier. When the compiler connection, the project will integrate all the symbols together, because the file has the same name variables, so it was a double definition error. 
Each header file to add conditional compilation to avoid being repeatedly explained that the file is referenced more than once, this is should be a habit. This approach would solve most of the low-level problems.
Conditional compilation example
#ifndef TEST_H
#define TEST_H
......
#endif

2. Use the extern define global variables:
you can put all global variables into a header file global.h (name at random from, but to add conditional compilation), each front plus extern variable, statement about these variables in other files defined. And then create a header file name of the corresponding .c or .cpp file as global.c. Declare all global variables inside. For example: void (* Handl_Display) () ;
and then, let the files related to the global variables include "global.h". So the compiler, the compiler will generate a first for global.c global.o, then .o and links to other files to produce an executable file.
Simply put, that is, before adding variables extern, after the declaration of variables in .c in.

 

3. Use Static modified:
add static in front of variables declared as static variables.
Although this method can solve the problem of multiple definition, but it can cause other problems.
Questions are as follows:
three files, ah, ac, bc;
in both ac and bc include the ah.
Call function ac bc in the middle of the variable assignment ah, but in fact the variables bc still have not been assigned.
Analysis:
static meaning is to force that variable is only visible in a file. 
Suppose you define static x in the header file; 
and the header files are included ac and bc;
in essence defines a variable named x, respectively, in ac and bc, the two x nothing to do.
Modify x in the ac, he will not cause bc where x changes.

Guess you like

Origin www.cnblogs.com/Zhangyq-yard/p/10944793.html