Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 错误:';键入名称';声明为返回函数的函数_C - Fatal编程技术网

C 错误:';键入名称';声明为返回函数的函数

C 错误:';键入名称';声明为返回函数的函数,c,C,我正在使用avr-gcc为PID控制器开发一个小型库。尽管在头文件中声明了函数并在.c文件中单独定义了函数,但编译器仍会抛出以下错误: Compiling C: pid.c avr-gcc -c -mmcu=atmega16 -I. -gdwarf-2 -DF_CPU=1000000UL -Os -funsigned-char - funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-pro

我正在使用avr-gcc为PID控制器开发一个小型库。尽管在头文件中声明了函数并在.c文件中单独定义了函数,但编译器仍会抛出以下错误:

Compiling C: pid.c
avr-gcc -c -mmcu=atmega16 -I. -gdwarf-2 -DF_CPU=1000000UL -Os -funsigned-char -                 
funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-  
adhlns=./pid.lst  -std=gnu99 -MMD -MP -MF .dep/pid.o.d pid.c -o pid.o 
pid.c:5: error: expected declaration specifiers or '...' before '(' token
pid.c:5: warning: function declaration isn't a prototype
pid.c:5: error: 'type name' declared as function returning a function
pid.c:5: error: conflicting types for 'PID_init'
pid.h:23: error: previous declaration of 'PID_init' was here
pid.c: In function 'PID_init':
pid.c:5: error: parameter name omitted
pid.h的头文件内容如下:

#include<avr/io.h>
#include<util/delay.h>

#ifndef PID_CONTROLLER
#define PID_CONTROLLER
struct PIDCONTROL

{

float error;
float prev_error;
float Kp;
float Ki;
float Kd;                       
float pid;
float P;
float I;
float D;
float setpoint;

};

void PID_init(float,float,float,float,struct PIDCONTROL*);

float PID(float,struct PIDCONTROL*);                        

#endif
#包括
#包括
#ifndef PID_控制器
#定义PID\U控制器
结构控制
{
浮动误差;
浮动前误差;
浮动Kp;
浮Ki;
浮动Kd;
浮动pid;
浮动P;
浮点数I;
浮动D;
浮动设定点;
};
void PID_init(float,float,float,float,struct PIDCONTROL*);
浮点PID(浮点,结构PID控制*);
#恩迪夫
已声明函数的定义已在pid.c中定义,其中包含以下代码:

#include<avr/io.h>
#include<util/delay.h>
#include "pid.h"

void PID_init(float SP,float Kp,float Ki,float Kd,struct PIDCONTROL *a)

{

a->Kp=Kp;

a->Ki=Ki;

a->Kd=Kd;

a->pid=0;

a->setpoint=SP;

a->prev_error=0;

}


float PID(float PV,struct PIDCONTROL *a)

{

a->error=(a->setpoint)-PV;

a->P=(a->Kp)*(a->error);

(a->I)+=(a->Ki)*(a->error)*1.024;

a->D=(a->Kd)*((a->error)-(a->prev_error))/1.024;

a->pid=(a->P)+(a->I)+(a->D);

a->prev_error=a->error;

return(a->pid);

}
#包括
#包括
#包括“pid.h”
无效PID_init(浮点SP、浮点Kp、浮点Ki、浮点Kd、结构PID控制*a)
{
a->Kp=Kp;
a->Ki=Ki;
a->Kd=Kd;
a->pid=0;
a->设定点=SP;
a->prev_错误=0;
}
浮动PID(浮动PV,结构PID控制*a)
{
a->错误=(a->设定点)-PV;
a->P=(a->Kp)*(a->错误);
(a->I)+=(a->Ki)*(a->error)*1.024;
a->D=(a->Kd)*((a->error)-(a->prev_error))/1.024;
a->pid=(a->P)+(a->I)+(a->D);
a->prev_error=a->error;
返回(a->pid);
}

我不知道代码出了什么问题。感谢您的帮助。请提前感谢。

avr/io.h文件还引入了
avr/common.h
,其中包含以下小片段:

/*
    Stack pointer register.
        AVR architecture 1 has no RAM, thus no stack pointer.
        All other architectures do have a stack pointer. Some devices have only
            less than 256 bytes of possible RAM locations (128 Bytes of SRAM
            and no option for external RAM), thus SPH is officially "reserved"
            for them.
*/

# ifndef SP
    # define SP _SFR_MEM16(0x3D)
# endif
(事实上,这有点复杂,有多条路径取决于您的体系结构,但这个简化的示例至少说明了实际问题所在)

它实际上是为
SP
定义一个预处理器宏,这意味着,当您试图在代码中使用该宏时,函数定义如下:

void PID_init(float SP,float Kp,float Ki,float Kd,struct PIDCONTROL *a)
实际上,您将得到如下结果:

void PID_init(float _SFR_MEM16(0x3D),float Kp,float Ki,float Kd,struct PIDCONTROL *a)
这将导致编译器阶段痛苦地抱怨


实际上,我非常相信合理的自描述变量名,因此通常会选择比
SP
更详细的变量名,但您可能会发现,即使您使用
mySP
,它也会解决眼前的问题。

尝试为结构添加typedef,在函数定义中使用它作为参数?您可能希望运行gcc-E来展开所有宏,将结果放入.c文件中,然后进行编译,以防在某个头中定义了一个标识符。最佳实践还要求将你的#include放在你头部的include防护中;否则,如果标题被多次包含,gcc无法优化为只读取一次。您的include-guard应该被称为PID\u-CONTROLLER\H,这样它就不可能与类型或变量名一致。@roelofs:没有理由认为这会有帮助。在我删除了
\include
指令后,它对我来说编译没有错误。很可能其中一个标题有问题。(顺便说一句,
pid.h
的全部内容,包括
#include
指令,可能都应该在include保护中;但这不是问题所在。)@KeithThompson一个很好的理由是,如果您输入了一个打字错误(例如
struct PIDCOTNROL
),它会被视为新结构类型的隐式声明;但是,如果您在typedef版本中输入了一个错误,则会立即得到一个未声明标识符的错误