[英]C++ struct and function declaration. Why doesn't it compile?
This compiles fine (Arduino):这编译得很好(Arduino):
struct ProgressStore {
unsigned long ProgressStart;
unsigned long LastStored;
uint32_t FirstSectorNr;
};
void IRAM_ATTR ProgressInit(ProgressStore aProgressStore){
}
Leave out the IRAM_ATTR and it doesn't compile anymore(?):省略 IRAM_ATTR 并且它不再编译(?):
Verbruiksmeter:116:6: error: variable or field 'ProgressInit' declared void
116 | void ProgressInit(ProgressStore aProgressStore){//, uint32_t SectorNr) {
| ^~~~~~~~~~~~
Verbruiksmeter:116:19: error: 'ProgressStore' was not declared in this scope
116 | void ProgressInit(ProgressStore aProgressStore){//, uint32_t SectorNr) {
| ^~~~~~~~~~~~~
See here: https://stackoverflow.com/a/17493585/2027196见这里: https://stackoverflow.com/a/17493585/2027196
Arduino does this mean thing where it finds all of your function definitions in main, and generates a function declaration for each above the rest of your code. Arduino does this mean thing where it finds all of your function definitions in main, and generates a function declaration for each above the rest of your code. The result is that you're trying to use ProgressStore before the ProgressStore struct is declared.
结果是您在声明 ProgressStore 结构之前尝试使用 ProgressStore。 I believe the IRAM_ATTR must suppress this behavior.
我相信 IRAM_ATTR 必须抑制这种行为。
It ends up generating this before compilation:它最终在编译之前生成:
void ProgressInit(ProgressStore aProgressStore); // <-- ProgressStore not yet declared
struct ProgressStore {
unsigned long ProgressStart; //Value of the counter at the start of the sector
unsigned long LastStored; //Should be new CounterValue-1, but you never know...
uint32_t FirstSectorNr; //1st of 2 sectors used for storage of progress
};
void ProgressInit(ProgressStore aProgressStore) {//, uint32_t SectorNr) {
// ProgressStore.1stSector = SectorNr;
}
One solution is to move your structures and classes into their own .h
files, and include those at the top.一种解决方案是将您的结构和类移动到它们自己的
.h
文件中,并将它们包含在顶部。
ProgressStore.h ProgressStore.h
#ifndef PROGRESS_STORE_H
#define PROGRESS_STORE_H
struct ProgressStore {
unsigned long ProgressStart; //Value of the counter at the start of the sector
unsigned long LastStored; //Should be new CounterValue-1, but you never know...
uint32_t FirstSectorNr; //1st of 2 sectors used for storage of progress
};
#endif // PROGRESS_STORE_H
main.cpp主文件
#include "ProgressStore.h"
void ProgressInit(ProgressStore aProgressStore) {//, uint32_t SectorNr) {
// ProgressStore.1stSector = SectorNr;
}
The function declaration is still auto-generated, but inserted after your #include
s function 声明仍然是自动生成的,但在您的
#include
s 之后插入
//, uint32_t SectorNr) {
you commented out a necessary part of the code ) {
您注释掉了代码的必要部分
) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.