Home  >  Article  >  Backend Development  >  C++ compilation error: function call does not match function declaration, how to solve it?

C++ compilation error: function call does not match function declaration, how to solve it?

WBOY
WBOYOriginal
2023-08-22 12:39:221750browse

C++ compilation error: function call does not match function declaration, how to solve it?

C Compilation error: The function call does not match the function declaration. How to solve it?

When developing C programs, you will inevitably encounter some compilation errors. One of the common errors is that the function call does not match the function declaration. This kind of error widely exists among C programmers. Due to not paying attention to the correctness of function declaration, it leads to compilation problems, which ultimately wastes time and energy to fix the problem and affects development efficiency. Ways to avoid this mistake require following some norms and standard practices, let’s take a look at them below.

What is the error that the function call does not match the function declaration

The error that the function call does not match the function declaration usually manifests itself as the compiler cannot match the call of the function to its declaration. This situation usually occurs in the following situations:

  1. The function declaration does not match the defined parameters.
  2. The function declaration and defined return type do not match.
  3. The C compiler cannot find the function declaration or definition.
  4. There are duplicate declared functions.

How to solve the error that the function call does not match the function declaration

To solve the error that the function call does not match the function declaration, you need to follow the following steps:

  1. Check the function Whether the declared and defined parameters match

When a function call does not match the function declaration error occurs, you first need to check whether the declared and defined parameters of the function match. If they don't match, you need to change the parameter type in the function declaration or change the parameter type in the function definition to make it consistent with the declaration.

Example:

Declaration: int function(int a, int b);

Definition: int function(int a, float b){}

In the above example, the function declaration has two integer parameters, while the function definition has one integer and one floating point parameter, so the second parameter type of the function definition needs to be changed to an integer.

  1. Check whether the function declaration and the defined return type match

When the return type does not match, the compiler will generate an error that the function call does not match the function declaration. In this case, you need to check whether the function definition and the declared return type are the same. If they are not the same, you need to change the return type in the function definition and declaration to make them the same.

Example:

Declaration: float function(float a, float b);

Definition: int function(float a, float b){}

In the above example, the function declaration returns a floating point number, while the function definition returns an integer. The function definition needs to be changed to return a floating point number.

  1. Check that the names and parameters of the function declaration and definition are correct

If the name and parameters of the function declaration and definition are not wrong, but the compiler still reports the function call and function Declaration mismatch error, it may be because it cannot find the function declaration or definition. In this case, it may be necessary to recheck file inclusion and linking.

Example:

The declaration and definition are correct, but there is still an error that the function call does not match the function declaration:

include "function.h"

int main()
{

int result = foo(3,4);
return 0;

}

In this case, you need to check whether the header file function.h is included correctly.

  1. Check if there are duplicate declared functions

In C, functions can be declared in multiple locations, including source files, header files, and library files. Multiple declarations may cause compile-time errors and result in function calls that do not match function declarations. Therefore, the file needs to be checked for duplicate declarations so that appropriate changes or deletions can be made.

Conclusion

Errors in inconsistencies between function calls and function declarations may cause errors in the C compiler, thus affecting the normal operation of the program. Therefore, programmers need to follow some specifications, including parameter and return type matching, including header files correctly and checking whether there are duplicate declarations in files, etc., to avoid or solve such errors. If any compilation errors are found, they need to be carefully checked and solved step by step to ensure the accuracy and correctness of the program.

The above is the detailed content of C++ compilation error: function call does not match function declaration, how to solve 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