2017-01-07 71 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