簡體   English   中英

C++ 結構和 function 聲明。 為什么不編譯?

[英]C++ struct and function declaration. Why doesn't it compile?

這編譯得很好(Arduino):

struct ProgressStore {
  unsigned long ProgressStart; 
  unsigned long LastStored;    
  uint32_t FirstSectorNr;      
};

void IRAM_ATTR ProgressInit(ProgressStore aProgressStore){
}

省略 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) {
 

 |                   ^~~~~~~~~~~~~

見這里: 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. 結果是您在聲明 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

主文件

#include "ProgressStore.h"

void ProgressInit(ProgressStore aProgressStore) {//, uint32_t SectorNr) {
//  ProgressStore.1stSector = SectorNr;
}

function 聲明仍然是自動生成的,但在您的#include s 之后插入

//, uint32_t SectorNr) {

您注釋掉了代碼的必要部分) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM