Undeclared identifier 'red'.

I'm not understanding why return red, green, and blue gives me the error that there is an undeclared identifier. Also, my checkRange function should make sure the value is between 0 and 255, and return the closest value to 0 and 255. Does my function look correct?


int Color::checkRange(int val) {
if ((val >= 0) && (val <= 255)) {
return val;
}
else {
if ((val - (255 - 1)) > val) {
return 0;
}
else {
return (255 - 1);
}
}
}

//Default constructor
Color::Color() {
red = 0;
green = 0;
blue = 0;
}

void Color::setRed(int redVal) {
red = redVal;
}

int getRed() {
return red;
}

void Color::setGreen(int greenVal) {
green = greenVal;
}

int getGreen() {
return green;
}

void Color::setBlue(int blueVal) {
blue = blueVal;
}

int getBlue() {
return blue;
}
Do you declare red, green and blue as int variables somewhere in your class definition? I see where you assign them a value in a constructor, but I don't see the actual declaration of these class data members.
If, as @wildblue speculated, your red, green and blue variables are part of the Color class, then you can only access them with Color class member functions. Your getRed function (for instance) must be a member of Color. So, the definition would be:

int Color::getRed() { return red; }
Topic archived. No new replies allowed.