我试图在我的微 Controller 上实现一个配置读取器和写入器(使用 AVR-GCC),但遇到了一些编译错误,特别是:
error: expected constructor, destructor, or type conversion before '(' token
error: expected constructor, destructor, or type conversion before '.' token
在我的主文件中。造成这种情况的代码是:
#include "cfg.h"
struct config_t* config;
config.temp = TEMP_C;
config.precision = 9;
config.time = 0;
config_read(config);
cfg.h
的内容:
#ifndef CFG_h
#define CFG_h
#include <avr/eeprom.h>
#include <inttypes.h>
#define TEMP_C 0
#define TEMP_F 1
#define TEMP_K 2
typedef struct config_t {
uint8_t temp;
uint8_t precision;
int32_t time;
} config_t;
void config_read(struct config_t * config);
void config_write(const struct config_t * config);
#endif
和cfg.c
的内容:
#include "cfg.h"
void config_read(struct config_t* config) {
eeprom_read_block((void*)&config, (void*)(0), sizeof(config_t));
}
void config_write(const struct config_t* config) {
eeprom_write_block((const void*)&config, (void*)(0), sizeof(config_t));
}
最佳答案
struct config_t* config;
问题:config 是未初始化的变量。您需要使用 malloc 为 config_t 结构创建一个对象
config.temp = TEMP_C;
config.precision = 9;
config.time = 0;
问题:由于 config_t 是一个指针,您可以使用“->”运算符而不是“.”运算符来访问 config_t 结构的方法或变量。
喜欢:
config->temp = TEMP_C;
config->precision = 9;
config->time = 0;
config_read(config);
关于c - 声明结构指针时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24732579/