Pushing a struct array to a stack

Hello everyone,

I am trying to push something like a mixed array to a stack. That means I want to push an array with a string and an Integer, but I get the following error message:

 no matching function for call to 'StackArray<String []>::push(runThrough(String*, int)::command_type [2])'

This is my code:

#include <StackArray.h>

//...

String runThrough(String lines[], int lnnum) {
  String res = "";
  StackArray <String> commands;

  for (int i = 0; i < lnnum; i++) {
    if (isCommand(lines[i]) && lines[i].substring(4, 5) == "0") {
      typedef struct {
        String com;
        int pos;
      } command_type;
      
      command_type command[2];
      command[0].com = lines[i];
      command[0].pos = i;

      commands.push(command);
    }
  }

  //...

  }

  return res;
}

I am using this stack-library: Arduino Playground - StackArray Library

"lines" is an array which consists of multiple bytes in the form of a String, because this option is more comfortable for me. "lnnum" is the length of that array.
I want to push lines[i ] and i to the stack.

The only solution that would come to my mind is to convert i to a String, but that wouldn't be that elegant.

Is there any way to push a struct array or two variables of different data types at the same time to a stack?

Thanks for your help and stay healthy!

StackArray is a template class and you have declared it like this:

  StackArray<String> commands;

Therefore you can only push items that are of type String to the commands stack.

Also, it is preferred not to use a typedef for struct. Rather declare it like this:

      struct command_type {
        String com;
        int pos;
      };

If you want to push a command onto the commands stack then move your struct declaration outside of the block and declare StackArray using the command_type struct like this:

  struct command_type {
     String com;
     int pos;
  };
  StackArray<command_type> commands;

Ok I did this. My Code looks like that now:

String runThrough(String lines[], int lnnum) {
  String res = "";

  struct command_type {
    String com;
    int pos;
  };
  
  StackArray <command_type> commands;

  for (int i = 0; i < lnnum; i++) {
    if (isCommand(lines[i]) && lines[i].substring(4, 5) == "0") {      
      command_type command[2];
      command[0].com = lines[i];
      command[0].pos = i;

      commands.push(command);
    }
  }

  //... 

  return res;
}

But I still get an error message:

no matching function for call to 'StackArray<runThrough(String*, int)::command_type>::push(runThrough(String*, int)::command_type [2])'

Not sure why you are declaring a 2 element array of command when you are only using the first element. However, you are trying to push the array command, rather than a single element. Try this:

      commands.push(command[0]);

Thanks! This worked.

Okay, I got a new problem:

As I tried to use command_type and assign a value to command[0].com and command[0].pos, I got again an error message:

no match for 'operator[]' (operand types are 'command_type' and 'int')

Here is the relevant Code:

struct command_type {
  String com;
  int pos;
};

void setup() {
  command_type command;
  command[0].com = "Hello World";
  command[0].pos = 10;
}

Please help!
Thanks alot...

'command' is not an array. Why are you trying to index it like one?

Okay I changed it. I don't know why I indexed it like an array. I probably saw it somewhere and then copied it without knowing what it meant.