This article resolves the error of multiple definition of ' XX '. " for the purposes of anti-reptiles, you are not seen on the http://blog.csdn.net/zhanh1218 , certainly not the latest and most complete." "
For the definition of the header file, be sure to add the following code (this is the header file protector):
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:12PX;" > #ifndef person_h_#define person_h_//Your code block #endif/* Person_h_ */</span></span>
Person_h_ is the name of the protector, the general recommendation is consistent with the class name! My class name is Person.h in the sample.
Whenever the compiler encounters # include, it is necessary to compile the relevant code block, but the compiler does not know whether it has been compiled, assuming that the compilation has been compiled, it is not equal to the code block written two times? Therefore, there is a need to do not repeatedly compile the mechanism, and this mechanism is formally above the code.
The detailed implementation is: #ifdef当且仅当变量已定义时为真, #ifndef当且仅当变量没有定义时为真. Once the test results are true, the operation may run until the #endif is encountered.
In other words: Assuming that the first include "Person.h", Person_h_ is undefined, at this point, the compiler will define this protector and run the code block compilation! Until you meet #endif. The next time you encounter this protector, you will not run the code block compilation. This mechanism guarantees that the compilation will not be repeated.
in practical use, I found that a single CPP file multiple include in the same. h header file or header file several times include a header file, there is no problem. However, this can be problematic when multiple CPP files include the same. h header file. The problem is definition of Non-static and non-inline will also report multiple definition of ' XX ' error. "That is: #define Span lang= "en-US" style= "font-family:arial; color:red; line-height:26px ">.cpp, < Span style= "font-family: ' Times New Roman '" >.cpp file "
Finally, the solution is to define the declaration of the class in the header file and the body definition of the class (that is, the contents of {} ), and define the implementation of the out-of- class function in a. cpp file of the same name. Problem solved perfectly. So, even the book written by the Great God is not entirely right, or all of it is clear.
So why would a header file be defined in the form below? Rather than just stating that the class body is not defined?
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:12PX;" >class a{//class definition};</span></span>
the definition of a class simply tells the compiler what the data format of the class is, and how much space the object should occupy after the instance session. the definition of a class also does not produce the target code. So the only difference between it and the declaration of a normal variable is that it cannot occur multiple times within the same compilation unit.
Another reason is that classes can be redefined in multiple. cpp files, but not variables, except for variables modified with extern or Staic.
As for the common variable: the variable that agrees with the static type definition , agree to the extern declaration ( cannot be defined! ); direct declaration such as int A; it is not possible, but also repeatedly defined.
extern indicates that the variable has already been defined elsewhere, and that variable is used here; Static variables, when allocating memory, are stored in a static area and are not stored on the stack. "A good blog: Click to open the link"
The following is a sample code demonstration, which is part of C + + primer exercises. "Anti-crawler, update the code the next day!" 】
/************ * File_name:vector_test.cpp * * Created on:2014 June 28 pm 3:3 4:23 * Author:the_third_wave, blog:http://blog.csdn.net/zhanh1218 * Email: [email protected] * Last modified:2014 June 28 pm 3:34:23 *********************************************************************/#include < iostream> #include <vector> #include <string> #include "headers/person.h" extern int k;int main () {std:: Vector<person> per = {"The_third_wave", +, "blog:http://blog.csdn.net/zhanh1218"}}; Class initialization +vector, so {{}, {}} must have a for (auto &p:per) {print (std::cout, p);}}
/************ * File_name:ddqdq.cpp * * Created on:2014 June 28 10:28:42 * Author:the_third_wave, blog:http://blog.csdn.net/zhanh1218 * Email: [email protected] * Last Modif ied:2014 June 28 pm 10:28:42 *********************************************************************/#include < iostream> #include <vector> #include "headers/person.h" extern int k;
/********************************************************************* * file_name:person.h * * Created on:2014 year June 28 Day Afternoon 11:47:08 * author:the_third_wave, blog:http://blog.csdn.net/zhanh1218 * Email: [email protected] * Last modified:2014 June 28 11:47:08 *********************************************************************/# Ifndef person_h_#define person_h_/*****************************begin***********************************/#include <iostream> #include <string>using namespace Std;extern int a;class person{friend IStream &read (IStream &is, person &item); Friend Ostream &print (ostream &os, const person &item);p Ublic:person () = default; Person (const string &n, unsigned int. A, string add): Name (n), age (a), address (add) {} person (IStream &); String name () const {return Name;} unsigned int age () const {return age;} String address () const {return Address;} private:string name = ""; Unsigned int age = 1; string address = "";}; Inlineperson::P Erson (IStream &is) {Read (is, *this);} /******************************end************************************/#endif/* person_h_ */
/********************************************************************* * file_name:Person.cpp * * Created on: June 28, 2014 PM 10:55:32 * author:the_third_wave, blog:http://blog.csdn.net/zhanh1218 * Email: [Email protected ] * Last modified:2014 June 28 10:55:32 *********************************************************************/# Include <iostream> #include <string> #include "Person.h" using namespace Std;istream &read (IStream & is, person &item) {is >> item.name >> item.age >> item.address; return is;} Ostream &print (ostream &os, const person &item) { os << item.name << "" << Item.age < < "<< item.address << Endl; return OS;}
Please leave a message if you don't understand.
This article by @the_third_wave (blog address: http://blog.csdn.net/zhanh1218) original. There are not covered, will be updated periodically, there are errors please correct me.
If you see this blog post is not complete, that is I to prevent the crawler first to announce half of the reasons, please see the original author blog.
Let's say this blog post is helpful for you, for a good network environment, not recommended reprint, recommended collection! If you must reprint, please bring the suffix and the address of this article.
C + +: multiple. cpp files include the same. h header File Definition method