Home >Backend Development >C++ >C++ error: multiple definitions, how to modify it?

C++ error: multiple definitions, how to modify it?

王林
王林Original
2023-08-21 20:31:441370browse

C is a high-level programming language that is widely used especially in computer science. However, when writing programs, we often encounter error reports, such as "multiple definitions". What should we do at this time?

First of all, we need to understand why "multiple definitions" occur. In C, if we define the same variable, function or class in different files, the problem of "multiple definitions" will occur.

So what should we do if this happens in our program? Usually, there are two solutions.

The first method is to use a "header file", that is, write the definition content in a separate file, and then reference this file through the #include directive in the required file. In this way, the header file only needs to be referenced once in each file to avoid the "multiple definition" problem.

Taking a function as an example, we can define the prototype of the function in a header file, and then reference this header file through the #include directive in the required files. For example, we can define the function "int add(int a, int b);" in the header file "function.h", and then reference this header file in the required file by #include "function.h", in the file Just call the function "add(a, b)".

Code example:

function.h:

#ifndef FUNCTION_H
#define FUNCTION_H

int add(int a, int b);

#endif

function.cpp:

#include "function.h"

int add(int a, int b)
{
    return a + b;
}

main.cpp:

#include <iostream>
#include "function.h"

int main()
{
    int a = 3, b = 5;
    std::cout << add(a, b) << std::endl;
    return 0;
}

is in use In the case of "header files", you only need to reference the header file once in each file, and you can use the function "add(a, b)" in the program to avoid the problem of "multiple definitions".

The second method is to use "namespace". Through namespaces, we can specify different names for different variables, functions, or classes to avoid the "multiple definitions" problem caused by definitions with the same name.

Code example:

namespace FirstNamespace {
    int value = 1;
}
 
namespace SecondNamespace {
    int value = 2;
}
 
int main()
{
    std::cout << FirstNamespace::value << std::endl;
    std::cout << SecondNamespace::value << std::endl;
    return 0;
}

In the above code, we defined the variable "value" with the same name in the "FirstNamespace" and "SecondNamespace" namespaces respectively. When using, we distinguish different variables through namespaces. In this way, even if variables with the same name are defined in different files, the problem of "multiple definitions" can be avoided through namespaces.

To sum up, when "multiple definitions" occur in the program, we can use "header files" or "namespaces" to solve this problem. We need to choose the appropriate method according to the specific situation, and be careful to avoid definitions with the same name in the program.

The above is the detailed content of C++ error: multiple definitions, how to modify it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn