简体   繁体   中英

Struct & Function - Does not name a type

I'm trying to convert a Hex Colour String into a RGB value using a function and struct, to then return the data

I've managed to do most of the work, but I'm struggling a bit to understand how exactly my Struct and Function should work together.

Here is my code that returns the error 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();
}

The line that it is erroring about is:

RGB getRGB(String hexValue) {

Could someone explain what I have done wrong and how to resolve it please?

If you are using a C compiler (as opposed to C++) you either have to typedef your struct or use the struct keyword wherever you use the type.

So it's either:

typedef struct RGB {
  byte r;
  byte g;
  byte b;
} RGB;

and then:

RGB theseColours;

or

struct RGB {
  byte r;
  byte g;
  byte b;
};

and then:

struct RGB theseColours;

However, if you are using a C++ compiler, then it may help if you tell us on what line the error occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM