2017-01-07 28 views
1

我想使用的功能和结构,以然后返回结构与功能 - 没有指定类型

我已经成功地完成大部分数据的十六进制颜色字符串转换成RGB值工作,但我努力了解一下我的Struct和Function应该如何一起工作。

这里是我的代码,返回错误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(); 
} 

它示数有关该生产线是:

RGB getRGB(String hexValue) { 

有人能解释我做了什么错误,以及如何解决它,请?

+0

编译器抱怨哪一行是代码?此外,语句'RGB值= {r,g,b}'在最后缺少分号。 –

回答

4

如果您使用的是C编译器(而不是C++),则必须键入您的结构或使用struct关键字,无论您使用何种类型。

所以它要么:

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

然后:

RGB theseColours; 

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

然后:

struct RGB theseColours; 

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

+0

对不起,我用显示错误的行更新了我的问题。这似乎是问题的两倍。 RGB被保留,我需要'''typedef struct''' – K20GH