Search code examples
c++arduinoarduino-unoarduino-ide

How to fix " 'Function' does not name a type "? [Arduino]


I'm making a scripting language for one of my Arduino projects, and I needed an eval function from python. So I spent forever coding one and I think I finally did it, but there's one issue, it won't work since I'm getting the error " 'evals' does not name a type.", even though I defined it earlier on in the sketch.

I've tried changing everything about the struct and nothing is working. (eg. I tried moving the * signs for the char variables, I tried changing the spacing, removing and adding back the ";" after the struct, adding and removing typedef before, etc..)

struct evals {
  char *pointer;
  bool boolout;
  char *charout;
  int intout;
  float floatout;
};
evals eval(String input) {
  evals output;
  String inputs = input;
  char input2[inputs.length() + 1];
  inputs.toCharArray(input2, inputs.length());
  if (input[0] == '"' and input[-1] == '"') {
    inputs.remove(0, 1);
    inputs.remove(-1, 1);
    output.pointer = "charout";
    char input2[inputs.length() + 1];
    inputs.toCharArray(input2, inputs.length());
    output.charout = input2;
    return output;
  } else if (input == "true") {
    output.pointer = "boolout";
    output.boolout = true;
    return output;
  } else if (input == "false") {
    output.pointer = "boolout";
    output.boolout = false;
    return output;
  } else {
    String inputss = inputs;
    inputss.replace("0", "");
    inputss.replace("1", "");
    inputss.replace("2", "");
    inputss.replace("3", "");
    inputss.replace("4", "");
    inputss.replace("5", "");
    inputss.replace("6", "");
    inputss.replace("7", "");
    inputss.replace("8", "");
    inputss.replace("9", "");
    if (inputss.length() == 0) {
      output.pointer = "intout";
      output.intout = inputs.toInt();
      return output;
    } else {
      if (inputss[0] == "." and inputss.length() == 0) {
        output.pointer = "floatout";
        output.floatout = inputs.toFloat();
        return output;
      } else {
        for (int Variable = 0; Variable < 50; Variable++) {
          if (LocalVariables[Variable] == "") {
            break;
          } else  {
            output.pointer = "variableout";
            output.intout = Variable;
            return output;
          }
        }
      }
    }
  }
}

I expect it to return a variable of type "evals" but it just gives that error.


Solution

  • You'd have to use struct evals instead of evals as the type, unless you specify something like:

    typedef struct evals evals;
    

    (This sets the type evals equal to the type struct evals)

    See the answer to this question for a decent explanation of why this is needed (TL;DR it's a holdover from c that doesn't make a ton of sense if you're new to the language).