Recently, a complex project has been integrated into an existing project. Compile-time found that some variable names conflict, the hint is xxxx ambiguous symbol, and in the compilation output, the specified two files in the specific variable name or the same class name. This compilation error occurs because two header files are loaded in the project, the two header files contain the same class name or variable name, and the method to resolve and avoid such errors is as follows:
- The code base is included in a namespace as much as possible. When we write a large program, some variables may have the same name as other projects, so it is necessary to add namespaces, in addition, the names of the class name variables named in their own code base try to start with the code base , which greatly reduces the likelihood of conflicts with other code library naming;
- use namespace xxx is forbidden in the header file. This is critical, the header file uses the namespace, then the file loaded with the header file, loaded the namespace, causing the header file pollution, the correct way is to add the header file, but before the specific class name using the domain identifier, such as std::string;
- There is also a lazy way to specify their domain identifiers directly in the conflict, but assuming that there are a lot of conflicts, then this method is cumbersome, it is recommended to use 1 and 2 to solve the problem from the root.
Solve the problem of class name and variable name conflict in ambiguous symbol namespace