Preprocessor Error? Having issues with a struct

I am having issues with Arduino 1.0.6 on a Mac (10.10.1).

It appears that the preprocessor is not recognizing my struct.

I am getting the following error:

Arduino: 1.0.6 (Mac OS X), Board: "Arduino Uno"
ChristmasArc:13: error: 'RGB' has not been declared
ChristmasArc:13: error: 'RGB' does not name a type
ChristmasArc.ino: In function 'void loop()':
ChristmasArc:48: error: 'nColor' was not declared in this scope
#include <math.h>
#include <Adafruit_NeoPixel.h>
#include <Arduino.h>

//Definitions
#define PIN 6
struct RGB {
  byte red;
  byte green;
  byte blue;
};

//Globals
float x1, x2, y1, y2, persistence;
int octaves;
boolean niceMood; // true = nice and false = naughty

unsigned int naughty, nice;
RGB nColor; // noisey RGB

// Initialize library
Adafruit_NeoPixel ring = Adafruit_NeoPixel(16, PIN, NEO_GRB+NEO_KHZ800);

void setup(){
  nice = true;
  ring.begin(); // initialize library
  ring.show(); // off
  persistence = 0.5;
  octaves = 3;
  
  // test series
  colorWipe(ring.Color(255, 0, 0), 50);
  colorWipe(ring.Color(0, 255, 0), 50);
  colorWipe(ring.Color(0, 0, 255), 50);
  colorWipe(ring.Color(0,0,0), 0);
  
  // set up fist color
  // Naughty is reddish and nice is cyanish
  naughty = 0; // red
  nice = 180; // cyan
}

void loop(){
  // calculate noise
  int noise;
  int hue;
  x1 = float(millis())/100.0;
  y1 = 10.0;
  for (uint16_t i=0; i<ring.numPixels(); i++){
    //
    noise = int(PerlinNoise2(x1, y1, persistence, octaves)*60);
    hue = ((niceMood)? nice : naughty) + noise;
    if (hue < 0) hue += 360;
    hue2rgb((unsigned int)hue, &nColor);
    ring.setPixelColor(i, ring.Color(nColor.red, nColor.green, nColor.blue));
  }
  ring.show();
}

void colorWipe(uint32_t c, uint8_t wait){
  //incrementally color the lights...
  for(uint16_t i=0; i < ring.numPixels(); i++){
    ring.setPixelColor(i, c);
    ring.show();
    delay(wait);
  }
}

Could you post the other two functions
PerlinNoise2 and hue2rgb?

Your struct should work as far as i can tell...

No, your struct will not work as you have it, and exactly as your error message says.

You declare your struct and only give it a tag name, so when you wish to create a variable of struct type RGB you need to tell it, it is a struct or change your definition to a typedef struct.

So, your code will need to look like this for it to compile:

//Definitions
#define PIN 6

struct RGB {
  byte red;
  byte green;
  byte blue;
};

//Globals
float x1, x2, y1, y2, persistence;
int octaves;
boolean niceMood; // true = nice and false = naughty

unsigned int naughty, nice;
struct RGB nColor; // noisey RGB

Paul

In C you would be correct. According to the C++ standard struct is identical in semantics with class you can change them at will. The ONLY difference between struct and class is that a struct defaults to public: whereas class defaults to private:

So is the Arduino preprocessor treating struct as a c-struct not a c++ struct?

BTW-- changing to struct RGB colors; gives the same errors.

It works for me

struct RGB {
  byte red;
  byte green;
  byte blue;
};

RGB myrgb;
void setup() {
  // put your setup code here, to run once:
RGB myrgb;
}

void loop() {
  // put your main code here, to run repeatedly:

}

I suspect the "RGB" is already defined in one of those .h file. Try changing its name.

Mark

I am wondering if the Arduino Preprocessor added a fwd declaration incorrectly. I originally had it named something differently then changed the name.

How do I check the processed source?

holmes4:
I suspect the "RGB" is already defined in one of those .h file. Try changing its name.

I tired changing it to mRGB and still the same errors.

Change:

RGB nColor; // noisey RGB

To:

struct RGB nColor; // noisey RGB

Or you could declare it at the same time you create the struct:

struct RGB {
  byte red;
  byte green;
  byte blue;
} nColor;

I fired up an old Arduino IDE 1.0.1 on the Mac mini running OSX 10.6.8 and this code compiles fine also for me.

struct RGB {
  byte red;
  byte green;
  byte blue;
};

RGB nColor; // noisey RGB

int myColour;

void setup(){
  nColor.blue = 128;
  nColor.red = 64;
  nColor.green = 196;
}

void loop(){
  myColour = nColor.red + nColor.green + nColor.blue;
}

Paul