【发布时间】:2015-12-07 13:10:47
【问题描述】:
我应该如何在头文件中声明这个联合?
#include <avr/io.h>
#include <stdint.h>
#include <stdio.h>
#include "i2c_twi.h"
#define DS3231_ADDR 0xd0
typedef union {
uint8_t bytes[7];
struct{
uint8_t ss;
uint8_t mm;
uint8_t hh;
uint8_t dayOfWeek;
uint8_t day;
uint8_t month;
uint8_t year;
};
} TDATETIME;
TDATETIME dateTime;
我这样声明,我不能在我的主函数中使用dataTime:
#include <stdint.h>
#ifndef DS3231_H_
#define DS3231_H_
typedef union _TDATETIME TDATETIME
#endif /* DS3231_H_ */
编译器产生以下错误:
../main.c:42:26: error: ‘dateTime’ undeclared (first use in this function)
DS3231_get_dateTime( &dateTime );
【问题讨论】:
-
你在哪里定义那个联合? n 源文件?为什么不在头文件中?您显示的错误消息是编译器显示的only消息吗?
-
typedef union _TDATETIME TDATETIME最后错过了;。 -
我不错过 ;在末尾。我不会误将它复制到这里。这是编译器显示的完整消息: ../main.c: In function ‘main’: ../main.c:42:26: error: ‘dateTime’ undeclared (first use in this function) DS3231_get_dateTime( &dateTime ); ^ ../main.c:42:26: 注意:每个未声明的标识符对于它出现在 make 中的每个函数只报告一次:*** [main.o]
-
你是要在main.c中定义
TDATETIME变量,还是访问union的成员?如果这样做,则需要在 header 中提供完整的 typedef。 -
第一个代码 sn-p 来自哪个文件?
标签: c struct header-files unions