且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++结构和函数声明。为什么它不能编译?

更新时间:2021-09-21 05:28:12

查看此处:https://***.com/a/17493585/2027196

Arduino做到了这一点,它在Main中找到您的所有函数定义,并为代码的睡觉上方的每个函数生成一个函数声明。结果是您试图在声明ProgressStore结构之前使用ProgressStore。我认为IRAM_Attr必须抑制此行为。

它最终在编译前生成以下代码:

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;
}

一种解决方案是将您的结构和类移动到它们自己的.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;
}

函数声明仍是自动生成的,但会插入#include%s