Home  >  Article  >  Backend Development  >  How to solve C++ compilation error: 'redefinition of 'function'?

How to solve C++ compilation error: 'redefinition of 'function'?

王林
王林Original
2023-08-27 14:27:131874browse

解决C++编译错误:\'redefinition of \'function\'\',如何解决?

Solution to C compilation error: 'redefinition of 'function', how to solve it?

C, as a powerful programming language, is often widely used in software development. However, writing error-free C programs is not easy for beginners. One of the common errors is "redefinition of 'function'", which is a function redefinition error. In this article I will explain the causes of this error and how to fix it.

Error reason:
When we define a function with the same name in a C program, the compiler will report a "redefinition of 'function'" error. This error usually occurs in the following situations:

  1. The same function is defined multiple times in the same source file.
  2. The same function is defined in different source files, and these source files are linked into the same program.

Look at the following example:

// example.cpp

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

int add(int a, int b) // 重复定义相同的函数
{
    return a + b;
}

int main()
{
    int result = add(3, 4);
    return 0;
}

In the above example, we have defined the function named "add" twice in the same source file. When we try to compile this program, we encounter a "redefinition of 'add'" error.

Solution:
There are two main ways to solve function redefinition errors: one is to avoid defining the same function multiple times in the same source file; the other is to define the same function in different source files Functions, use function declarations and header files to avoid conflicts.

  1. Avoid defining the same function multiple times in the same source file.
// example.cpp

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

int main()
{
    int result = add(3, 4);
    return 0;
}

The above code has fixed the function redefinition error. We just keep one function definition and call it in the main function.

  1. Use function declarations and header files when defining the same function in different source files.

First, let's create two source files: example.cpp and add.cpp.

// add.h

#ifndef ADD_H
#define ADD_H

int add(int a, int b);

#endif
// add.cpp

#include "add.h"

int add(int a, int b)
{
    return a + b;
}
// example.cpp

#include "add.h"

int main()
{
    int result = add(3, 4);
    return 0;
}

In the above example, we used the header file and function declaration to solve the function redefinition error. In the add.h header file, we define the declaration of the add function and use a conditional preprocessor to avoid repeated inclusion.

In the add.cpp source file, we implement the specific definition of the add function.

Finally, in the example.cpp source file, we include the add.h header file and can use the add function without function redefinition errors.

Summary:
Function redefinition error is one of the common errors in C development. To avoid this error, we should avoid defining the same function multiple times in the same source file, and use function declarations and header files to resolve conflicts caused by defining the same function in different source files. In this way we can write high-quality, error-free C programs.

The above is the detailed content of How to solve C++ compilation error: 'redefinition of 'function'?. 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