[SOLVED] size_t does not name a type

I am having difficulty with Arduino recognizing standard types. When compiling the code below I am getting the following error:

‘mx_sz’ does not name a type

By contrast, the uint8_t variable definition seems fine. I tried removing the hyphen but this made no difference. However it seems by contrast if I define this in void setup(), then I don’t get the error. However I do need this to be a global variable.

I am also getting the same for SerialMux. That could be down to some error in my class definition, but that shouldn’t affect size_t. I have included Ardiono.h in my class files as well as in the main file here.

#include <Arduino.h>

#include "SerialMux.h"

#define MXTTY0
//#define MXTTY1
//#define MXTTY2
//#define MXTTY3



uint8_t test = 0;

size_t mx_sz = 0;
mx_sz = 0;


#ifdef MXTTY0
  MxTTYobj tty0;
  tty0.tty.port = &Serial;
  tty0.tty.baud = 115200;
  tty0.tty.sw = (TTYIN^TTYOUT);
  mx_sz++;
#endif

#ifdef MXTTY1
    MxTTYobj tty1;
    tty1.tty.port = &Serial1;
    tty1.baud = 57600;
    tty1.sw = (TTYIN^TTYOUT);
    mx_sz++;
#endif

#ifdef MXTTY2
    MxTTYobj tty2;
    tty1.tty.port = &Serial2;
    tty2.sw = (TTYIN^TTYOUT);
    tty2.baud = 115200;
    mx_sz++;
#endif

#ifdef MXTTY3
    MxTTYobj tty3;
    tty3.tty.port = &Serial3;
    tty3.baud = 115200;
    tty3.sw = (TTYIN^TTYOUT);
    mx_sz++;
#endif

    MxTTYobj* muxPorts[mx_sz] = {
    #ifdef MXTTY0
      &tty0,
    #endif
    #ifdef MXTTY1
      &tty1,
    #endif
    #ifdef MXTTY2
      &tty2,
    #endif
    #ifdef MXTTY3
      &tty3
    #endif
    };

/*
 * Variables
 */
// Line end characters
static const uint8_t CR = 0x13;
static const uint8_t LF = 0x10;

// Initialise serial Mux ports
SerialMux serialMux(muxPorts, mx_sz);

void setup(){

  serialMux.flush();

}


void loop(){



}
mx_sz = 0;

You can't have an assignment statement outside of any function.

Thanks. I didn't know that. It seems that you can assign a default value with something like:

size_t mx_sx = 0;

but you can't subsequently change it outside of a function. Is there no way I can set up a counter to determine how many #ifdef MXTTYx objects I have defined so that pass the number of array elements to serialMux?

BitSeeker:
Thanks. I didn't know that. It seems that you can assign a default value with something like:

size_t mx_sx = 0;

That's not an assignment. It's a variable definition with initialization.

Don't know about your other question.

I'd probably use an array of MxTTYobj objects:

MxTTYobj tty[]  = {
  {&Serial, 115200, TTYIN ^ TTYOUT},
  {&Serial1, 115200, TTYIN ^ TTYOUT},
.
.
.
};

size_t mx_sz = sizeof(tty) / sizeof(tty[0]);

Modify you constructor accordingly.

Yes, this is creating a variable and “immediately assigning” the value of 0 to it. Technically, as you say, not an assignment. Sorry I was unclear in my terminology.

I think I have found a solution but am not getting:

request for member ‘init’ in ‘serialMux’ which is of non-class type ‘SerialMux’
serialMux.**i**nit(muxPorts, mx_sz);

Might still be something wrong in my class definition I suppose but I can’t see anything obvious. The object ‘serialMux’ is declared globally so presumably I should be able to access it and its members in setup()?

Supplying the parameters as an array within brackets looks rather cleaner as well. Will see what I can do with that. Thanks.

#include <Arduino.h>

#include "SerialMux.h"

#define MXTTY0
//#define MXTTY1
//#define MXTTY2
//#define MXTTY3


#ifdef MXTTY0
  MxTTYobj tty0;
#endif
#ifdef MXTTY1
  MxTTYobj tty1;
#endif
#ifdef MXTTY2
  MxTTYobj tty2;
#endif
#ifdef MXTTY3
  MxTTYobj tty3;
#endif

MxTTYobj* muxPorts[] = {
  #ifdef MXTTY0
    &tty0,
  #endif
  #ifdef MXTTY1
    &tty1,
  #endif
  #ifdef MXTTY2
    &tty2,
  #endif
  #ifdef MXTTY3
    &tty3
  #endif
  };

SerialMux serialMux();


/*
 * Variables
 */
// Line end characters
static const uint8_t CR = 0x13;
static const uint8_t LF = 0x10;

void setup(){

#ifdef MXTTY0
  tty0.tty.port = &Serial;
  tty0.tty.baud = 115200;
  tty0.tty.sw = (TTYIN^TTYOUT);
#endif
#ifdef MXTTY1
  tty1.tty.port = &Serial1;
  tty1.baud = 57600;
  tty1.sw = (TTYIN^TTYOUT);
#endif
#ifdef MXTTY2
  tty1.tty.port = &Serial2;
  tty2.sw = (TTYIN^TTYOUT);
  tty2.baud = 115200;
#endif
#ifdef MXTTY3
  tty3.tty.port = &Serial3;
  tty3.baud = 115200;
  tty3.sw = (TTYIN^TTYOUT);
#endif

  constexpr size_t mx_sz = sizeof muxPorts / sizeof *muxPorts;

  // Initialise serial Mux ports
  serialMux.init(muxPorts, mx_sz);

  serialMux.flush();

}


void loop(){



}

I think I figured it out. The line:

SerialMux serialMux();

should have been:

SerialMux serialMux;

Now compiles OK.