Home > Enterprise >  I've a small problem with the declaration of a function "declaration is incompatible with&
I've a small problem with the declaration of a function "declaration is incompatible with&

Time:10-31

I was writing a C program. This C program has to compute the solutions of a quadratic equation. The problem is that debugger showed me this kind of errors: "E0147", and "C22371". this is my program:

delta.c:

    #include "delta.h"
    #include "math.h"

    double delta(double a, double b, double c) {
    double delta = 0; 
    double power = pow(b, b); 
    delta = power - (4 * a * c); 
    return delta; 
} 

delta.h:

#if !defined DELTA_H
#define DELTA_H  

extern double delta(double a, double b, double c); 

#endif /* DELTA_H */

solutions.c:

#include "delta.h"
#include "solutions.h" 
#include "math.h"

double solutions (double a, double b, double c, double* x1, double* x2) {
double checking_the_delta = 0; 
checking_the_delta = delta(a, b, c); 
if (checking_the_delta == 0) {
    return 1; 
}
else if (checking_the_delta >= 0) {
    double tmp = delta(a, b, c);
    double square_root = sqrt(tmp);
    *x1 = (-b   square_root) / 2;
    *x2 = (-b - square_root) / 2;
}
else 
    return 0; 
}

solutions.h:

#if !defined SOLUTIONS_H 

#define SOLUTIONS_H

extern solutions(double a, double b, double c, double* x1, double* 
x2); 

#endif /* SOLUTIONS_H */

and finally, the main.c:

#include "solutions.h" 
#include "delta.h" 

int main(void) {
solutions(...); 
return 0; 
}

the problem is in this line: "double solutions (double a, double b, double c, double* x1, double* x2)". the debugger said this declaration is incompatible with the declaration in the header file. Moreover, it said that "solutions redefinition: different basic types".

I'm completely lost, I've written the declarations again but debugger keeps showing me these two errors.

CodePudding user response:

extern solutions(double a, double b, double c, double* x1, double* x2); fails to specify the return type of solutions. Typically, it will default to int, for reasons related to the history of C.

Then this is incompatible with double solutions (double a, double b, double c, double* x1, double* x2) because the latter declares the return type to be double.

Fix the declarations so they declare the same return type in both place: You might insert double after extern in the header declaration. However, it looks like the function is intended to return the number of distinct solutions. In that case, you may want to make the return type int in both places, and insert return 2; in the code that processes two solutions.

CodePudding user response:

For starters the function solutions returns nothing in case when checking_the_delta > 0 though the function return type is not void.

double solutions (double a, double b, double c, double* x1, double* x2) {
double checking_the_delta = 0; 
checking_the_delta = delta(a, b, c); 
if (checking_the_delta == 0) {
    return 1; 
}
else if (checking_the_delta >= 0) {
    double tmp = delta(a, b, c);
    double square_root = sqrt(tmp);
    *x1 = (-b   square_root) / 2;
    *x2 = (-b - square_root) / 2;
}
else 
    return 0; 
}

Also this declaration

double tmp = delta(a, b, c);

is redundant because the variable checking_the_delta already contains the result of the function call delta(a, b, c)

checking_the_delta = delta(a, b, c); 

Secondly in the function declaration in the header you forgot to specify the return type double as in the function definition

extern soluzioni(double a, double b, double c, double* x1, double* 
x2); 

Pay attention to that the function actually returns an integer instead of a double value. So maybe you need to specify the return type int.

  • Related