如何使用标头中声明的结构并由函数在源代码中定义

时间:2016-02-17 11:40:14

标签: c struct typedef

我试图将硬件配置的主要功能存储在一个地方,所以我为它创建了结构。我在头文件中将它们声明为typedef,我想创建和定义函数中的实际结构,我可以在应用程序启动时调用它。 (我正在使用运行chibiOS的STM32F04板。)

这是头文件:

#ifndef HW_INIT_INCLUDED
#define HW_INIT_INCLUDED

#include <mcuconf.h>
#include <hal.h>
#include <ch.h>

struct Recievers;
struct LEDs;
struct Ultrasonic;

typedef struct Recievers{
    int iD;
    int port;
    int pinNumber;
} Reciever;

typedef struct LEDs{
    int iD;
    char port;
    int pinNumber;
    bool status;
    float dutyCycleConstant;
} LED;

typedef struct Ultrasonic{
    char port;
    int pinNumber;
}Ultrasonic;


/*
Initaliases the values of hw parameters.
*/

void hardWareInit(struct Recievers Reciever, struct LEDs LED, struct Ultrasonic Ultrasonic);

#endif // HW_INIT_INCLUDED

这是源代码:

#include <hw_init.h>
#include <stdlib.h>


void hardWareInit(struct Recievers Reciever, struct LEDs LED, struct    Ultrasonic Ultrasonic)
{
    Reciever Reciever1, Reciever2, Reciever3, Reciever4;
    LED LED1, LED2, LED3;
    Ultrasonic Ultrasonic1;

    Reciever1.iD = 1;
    Reciever1.port = GPIOF;
    Reciever1.pinNumber = 1;

    Reciever2.iD = 2;
    Reciever2.port = GPIOG;
    Reciever2.pinNumber = 1;

    Reciever3.iD = 3;
    Reciever3.port = GPIOI;
    Reciever3.pinNumber = 1;

    Reciever4.iD = 3;
    Reciever4.port = GPIOE;
    Reciever4.pinNumber = 1;

    LED1.iD = 1;
    LED1.port = GPIOD;
    LED1.pinNumber = 16;
    LED1.status = 0;
    LED1.dutyCycleConstant = 0.3;

    LED2.iD = 2;
    LED1.port = GPIOD;
    LED2.pinNumber = 16;
    LED2.status = 0;
    LED2.dutyCycleConstant = 0.3;

    LED3.iD = 3;
    LED1.port = GPIOD;
    LED3.pinNumber = 16;
    LED3.status = 0;
    LED3.dutyCycleConstant = 0.3;

    Ultrasonic1.port = GPIOC;
    Ultrasonic1.pinNumber = 1;
}

当我尝试编译时,它基本上为我尝试创建的每个结构都提供了这些错误:

hw_init.c:8:5:警告:声明无效[-Wunused-value]      接收器1;      ^ hw_init.c:8:14:错误:预期&#39 ;;&#39;之前&#39; Reciever1&#39;      Reciever Reciever1;

1 个答案:

答案 0 :(得分:0)

  

当我尝试编译时,它基本上为我尝试创建的每个结构都提供了这些错误:

     

hw_init.c:8:5:警告:声明无效[-Wunused-value]接收者接收器1;   hw_init.c:8:14:错误:预期';'在'Reciever1'Reciever Reciever1之前;

您收到错误消息是因为您对typedef'd结构使用了单词Reciever以及第一个函数参数:

void hardWareInit(struct Recievers Reciever, struct LEDs LED, struct    Ultrasonic Ultrasonic)

您应该为第一个参数使用不同的指示符。

此外,我建议将reciever命名为receiver