结构和函数 - 不命名类型

K20*_*0GH 2 c++ arduino esp8266

我正在尝试使用函数和结构将十六进制颜色字符串转换为 RGB 值,然后返回数据

我已经成功完成了大部分工作,但我有点难以理解我的结构和函数应该如何协同工作。

这是我返回错误的代码RGB does not name a type

//Define my Struct
struct RGB {
  byte r;
  byte g;
  byte b;
};

//Create my function to return my Struct
 RGB getRGB(String hexValue) {
  char newVarOne[40];
  hexValue.toCharArray(newVarOne, sizeof(newVarOne)-1);
  long number = (long) strtol(newVarOne,NULL,16);
  int r = number >> 16;
  int g = number >> 8 & 0xFF;
  int b = number & 0xFF;

  RGB value = {r,g,b}
  return value;
}

//Function to call getRGB and return the RGB colour values
void solid(String varOne) {

  RGB theseColours;
  theseColours = getRGB(varOne);

  fill_solid(leds, NUM_LEDS, CRGB(theseColours.r,theseColours.g,theseColours.b));
  FastLED.show();
}
Run Code Online (Sandbox Code Playgroud)

它出错的行是:

RGB getRGB(String hexValue) {
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下我做错了什么以及如何解决它吗?

xan*_*oid 5

如果您使用的是 C 编译器(而不是 C++),则必须对结构进行 typedef 或在使用该类型的任何地方使用 struct 关键字。

所以它是:

typedef struct RGB {
  byte r;
  byte g;
  byte b;
} RGB;
Run Code Online (Sandbox Code Playgroud)

进而:

RGB theseColours;
Run Code Online (Sandbox Code Playgroud)

或者

struct RGB {
  byte r;
  byte g;
  byte b;
};
Run Code Online (Sandbox Code Playgroud)

进而:

struct RGB theseColours;
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用的是 C++ 编译器,那么告诉我们错误发生在哪一行可能会有所帮助。