Two struct problems - two workarounds

Given that all of the following code is declared globally before setup() in the Arduino IDE 1.8.5:

struct Steppr{
  int stepPin;
  int dirPin;
  int enablePin;
  long pos;

If I declare the variable and then try to assign a member value like this:

Steppr stA;
stA.stepPin = 46;

I get:
stepperTest:20: error: 'stA' does not name a type
stA.stepPin = 46;

The only way I've found to successfully declare this struct variable is like this:

Steppr stA = {46, 48, 62, 10000};
Steppr stB = {54, 55, 38, 10000};

So, the declaration works now.

Next problem: To use the struct variables in functions, everything works fine as long as the functions are defined in that same first tab. If those functions are moved to a later tab, in this case "d_moveElev" I get:

d_moveElev:1: error: 'Steppr' has not been declared
void moveElev (long there, Steppr sp) { //move elevator to received step position

This is true even if I declare the function in the first tab and define it in a later tab--doesn't compile.

After much experimentation, I found that if I moved even one function definition into the first tab, all the functions that use the same struct in later tabs compile without error. Hmm. So I used this hack in the first tab with success:

void structStepprHack (Steppr a) { // This allows struct Steppr to be used in function defs
  return;                    // in later tabs. It is never called in sketch.

I don't know why it works but all later-tabbed functions using the Steppr struct compile fine now.

You will have to show all your code (all tabs); also give the file names. A representative example is also fine.

The IDE (more exactly I think it's the builder) tries to be intelligent but sometimes drops a few stitches.

you can initialize your struct, but you cannot make an assignment like you are trying, unless you are in a function.


struct A {
  int a;
  int b;

A a = {1, 2};
//a.a = 1;  // no can do!!