Home > Software design >  C struct and function declaration. Why doesn't it compile?
C struct and function declaration. Why doesn't it compile?

Time:12-03

This compiles fine (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(?):

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

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

CodePudding user response:

See here: 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. The result is that you're trying to use ProgressStore before the ProgressStore struct is declared. I believe the IRAM_ATTR must suppress this behavior.

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.

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 #includes

CodePudding user response:

//, uint32_t SectorNr) {

you commented out a necessary part of the code ) {

  • Related