C++ struct as parameter to methods

I have some code that works in the Arduino IDE on various MCU platforms (Feather M0+, ESP8266 etc.). Similar code produces a compiler error in the Particle Web IDE.

error: variable or field 'updatePollResultDisplay' declared void
void updatePollResultDisplay(PollData pData);

I also noticed an error message later in the output from the compiler that points to the line on which the struct itself is declared

error: 'PollData' was not declared in this scope

I have a struct defined as

typedef struct PollData_tag
  int optionAValue;
  int optionBValue;
  int optionCValue;

the signature of the method in question looks like this

void updatePollResultDisplay(PollData pollData)

In a method that calls the above method I have code something like

PollData pollData;
pollData.optionAValue = 10;
pollData.optionBValue = 20;
pollData.optionCValue = 30;

All of the code shown above is in the same .ino file. I’m not a seasoned C++ programmer so I don’t quite understand the compiler’s error message nor what the best C++ practices are in this regard. It works in the Arduino IDE as I’d expect (as a C# programmer).

Any help and guidance would be appreciated.

That seems oneof the occasions where the Wiring preprocessor trips up and causes this.
Try adding this to the top of your sketch (and missing function prototypes if required)

#include "Particle.h"

@ScruffR. Dude, you’re incredible! I appreciate your quick responses tremedndously, thank you. I’ll try this out and let you know.

1 Like

Ok, that fixed it!
I only needed the


I didn’t need the include for Particle.h

Of course my code got turned “upside down” :smiley:. I understand the need for forward declarations. Is that the preferred C++ “style”?

Yup, C++ requires it - only the Wiring preprocessor rids you of that need to add convenience for less experienced coders

Aah, I see. So the Wiring preprocessor isn’t doing it’s job in the Particle Web IDE then? So Essentially, I could have all of my function prototypes declared and not have to include the #pragma. Just to stay sane, I should keep the pragma in there so I know I’m not relying on magic that works “sometimes”.

Not sure how to explain how it works in the Arduino IDE but not in the Particle Web IDE unless this is a particle Web IDE bug?

The Particle Wiring preproc usually does its job OK, just some code combos will trip it up.
Since the Particle IDEs are being pimped currently, we hope that’ll be fixed too.

Shouldn’t the signature be this:

void updatePollResultDisplay(PollData_tag pollData);

Yup, this is what I thought at first too, but for the typedef both is possible.

the struct part defines the actual structure (PollData_tag) and the typedef does define a type (PollData) of that shape.
Without the typedef, you’d get a PollData_tag PollData; with which you could do

PollData.optionAValue = 10;

Not so with the typedef as this doesn’t declare a variable but a type.

In fact PollData_tag is superfluous and can be left out completely.

Thanks Scruffr, was completely blind to the “typedef” keyword and just seeing the “struct” keyword only, if you know what I mean!

Yup, had the same thought and already started to type my first response that way, but half way through (and fortunatly before posting :blush:) realized my mistake.

In fact PollData_tag is superfluous and can be left out completely

Yea, the PollData_tag (the structure identifier or Tag Name) seems to be the C++ way of doing it, even though it is not really required. The identifier is required however, when you need a pointer to the struct before it has been typedef’ed. That is typically, within the struct itself. Kind of like a tree node, where is “Node” has a Node member.

So for my use, the structure identifier isn’t really required.

1 Like