c++ - 如何修复 "' Function' does not name a type "? [Arduino]

标签 c++ arduino arduino-uno arduino-ide

我正在为我的一个 Arduino 项目制作一种脚本语言,我需要一个来自 python 的 eval 函数。所以我花了很长时间编写一个代码,我想我终于做到了,但是有一个问题,它不会工作,因为我收到错误“'evals' 没有命名类型。”,即使我早些时候定义了它在草图中。

我已经尝试更改有关结构的所有内容,但没有任何效果。 (例如,我尝试移动 char 变量的 * 符号,我尝试更改间距,在结构之后删除和添加“;”,在之前添加和删除 typedef 等。)

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;
          }
        }
      }
    }
  }
}

我希望它返回一个类型为“evals”的变量,但它只是给出了那个错误。

最佳答案

您必须使用 struct evals 而不是 evals 作为类型,除非您指定如下内容:

typedef struct evals evals;

(这将类型 evals 设置为等于类型 struct evals)

查看 this question 的答案对于为什么需要这样做的合理解释(TL;DR 这是 c 的遗留问题,如果您是该语言的新手,则没有多大意义)。

关于c++ - 如何修复 "' Function' does not name a type "? [Arduino],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55988320/

相关文章:

node.js - 无法使用串行端口 Node JS 库从 Arduino Uno 读取数据

c++ - arr[0] 的大小怎么是 8 个字节?

C++ 将另一个方法作为参数传递

java - Arduino/Android远程控制

c++ - 在arduino项目中使用ArduinoCore-avr库

c - 在 Arduino 运行时向其发送命令

c++ - C++ atexit() 函数发生奇怪的崩溃

c++ - 使用 SFINAE 检查全局运算符<<?

url - Arduino 以太网客户端仅使用 IP 地址无法工作

c - 在 Arduino 中读取传感器时按下按钮