Juan Bravo Juan Bravo - 4 months ago 23
C Question

Defining a struct function

I have the next code, but qhen I compiled, it appear an erro: error: expected identifier or '(' before 'float'. But it is just exacly as other code i have and examples from Internet but I don't notice the error.

#ifndef Deteccion
#define Deteccion
#define acc_TH 12.74
int cont=0;
int pasos=0;
float SF,SL,SLT;

struct Deteccion
{
int muestras;
int pasos;
float frecuencia;
float longitud;
float orientacion;
};

struct Deteccion Detectar(float ax, float ay, float az, float wz)
{
float mag,longitud,frecuencia;
int muestras;
float sum_acc,sum_w,orientacion;
mag=(ax*ax)+(ay*ay)+(az*az);
mag=pow(mag,0.5);
if(mag>=acc_TH)
{
muestras=muestras+1;
sum_acc=mag+sum_acc;
sum_w=wz+sum_w;
}
else
{
cont=cont+1;
if(muestras<32)
{
muestras=0;
sum_acc=0;
sum_w=0;
}
}
if(cont==4)
{
pasos=pasos+1;
SF=50/muestras;
SL=sum_acc/muestras;
SL=pow(SL,0.333);
SL=0.22*SL;
SLT=SF+SL;
orientacion=(0.02*sum_w)+orientacion;
sum_w=0;
muestras=0;
sum_acc=0;
}
}
#endif

Answer

Since you are using Deteccion as the header guard, you may not use the same identifier for the struct tag. Preprocessor replaces Deteccion with blank space, so the compiler sees this:

struct // <<== Nothing
{
    int muestras;
    int pasos;
    float frecuencia;
    float longitud;
    float orientacion;
};

To fix this problem use a different identifier for the header guard:

#ifndef Deteccion_H
#define Deteccion_H

Note: It looks like you are using header guard inside a file that has function implementation. This is highly unusual. Consider restructuring your code so that implementations and declarations are placed in different files.

Comments