Creating Classes - "prototype .... does not match any in class"

So I’m not entirely sure what is wrong. Within my folder for Color, there is no other false file for Color.h or Color.cpp, and they should be able to link to each other.


Color.h

#ifndef COLOR_H
#define COLOR_H

#include <WString.h>
#include <HardwareSerial.h>
#include <Arduino.h>

class Color {

  public:

    // This constructor initializes the Color to black (r,g,b all zero)
    Color();

    // This constructor to initialize a particular color, the r, g, and b
    // arguments are to be assigned to _red, _green and _blue.
    Color(int r, int g, int b);

    int getRed() const;
    int getGreen() const;
    int getBlue() const;

    void setRed(int r);
    void setGreen(int g);
    void setBlue(int b);
    void setColor(int r, int g, int b);

    String getDominantColor() const;

  private:

    // each value is the "on-ness" of a color (0 to 255)
    int _red;
    int _green;
    int _blue;
};

#endif

Color.cpp

#include "Color.h"

Color::Color(int r, int g, int b) {
  _red = r;
  _green = g;
  _blue = b;

}

int Color::getRed() {

  return _red;
}

int Color::setRed(int r) {

  _red = r;

}

int Color::getGreen() {

  return _green;
}

int Color::setGreen(int g) {

  _green = g;

}

int Color::getBlue() {

  return _blue;
}

int Color::setBlue(int b) {

  _blue = b;

}


Color::setColor(int r, int g, int b) {
  _red = r;
  _green = g;
  _blue = b;
}

String Color::getDominantColor() {
  if (_red > _blue && _red > _green) {
    return "red";
  }
  else if (_blue > _red && _blue > _green) {
    return "green";
  }
  else if (_green > _blue && _green > _red) {
    return "green";
  }
  else {
    return "Indeterminate";
  }

}

Error message:

Arduino: 1.6.3 (Mac OS X), Board: "Arduino Uno"

Color.cpp:10:5: error: prototype for 'int Color::getRed()' does not match any in class 'Color'
 int Color::getRed() {
     ^
In file included from Color.cpp:1:0:
Color.h:21:9: error: candidate is: int Color::getRed() const
     int getRed() const;
         ^
Color.cpp:15:5: error: prototype for 'int Color::setRed(int)' does not match any in class 'Color'
 int Color::setRed(int r) {
     ^
In file included from Color.cpp:1:0:
Color.h:25:10: error: candidate is: void Color::setRed(int)
     void setRed(int r);
          ^
Color.cpp:21:5: error: prototype for 'int Color::getGreen()' does not match any in class 'Color'
 int Color::getGreen() {
     ^
In file included from Color.cpp:1:0:
Color.h:22:9: error: candidate is: int Color::getGreen() const
     int getGreen() const;
         ^
Color.cpp:26:5: error: prototype for 'int Color::setGreen(int)' does not match any in class 'Color'
 int Color::setGreen(int g) {
     ^
In file included from Color.cpp:1:0:
Color.h:26:10: error: candidate is: void Color::setGreen(int)
     void setGreen(int g);
          ^
Color.cpp:32:5: error: prototype for 'int Color::getBlue()' does not match any in class 'Color'
 int Color::getBlue() {
     ^
In file included from Color.cpp:1:0:
Color.h:23:9: error: candidate is: int Color::getBlue() const
     int getBlue() const;
         ^
Color.cpp:37:5: error: prototype for 'int Color::setBlue(int)' does not match any in class 'Color'
 int Color::setBlue(int b) {
     ^
In file included from Color.cpp:1:0:
Color.h:27:10: error: candidate is: void Color::setBlue(int)
     void setBlue(int b);
          ^
Color.cpp:44:36: error: ISO C++ forbids declaration of 'setColor' with no type [-fpermissive]
 Color::setColor(int r, int g, int b) {
                                    ^
Color.cpp:44:1: error: prototype for 'int Color::setColor(int, int, int)' does not match any in class 'Color'
 Color::setColor(int r, int g, int b) {
 ^
In file included from Color.cpp:1:0:
Color.h:28:10: error: candidate is: void Color::setColor(int, int, int)
     void setColor(int r, int g, int b);
          ^
Color.cpp:50:8: error: prototype for 'String Color::getDominantColor()' does not match any in class 'Color'
 String Color::getDominantColor() {
        ^
In file included from Color.cpp:1:0:
Color.h:30:12: error: candidate is: String Color::getDominantColor() const
     String getDominantColor() const;
            ^
Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Thank you for the assistance!

Hello and welcome,

In .h you have

void setRed(int r)

But in .cpp you have

int Color::setRed(int r)

should be

void Color::setRed(int r)

Thank you, I realized with that that the "const" modifier is also required after all of the "Color::className()".

I see no reason you couldn't inline the majority of the messaging functions and constructors and maybe limit the .cpp files contents to just 'getDominantColor' (given the current posting).

The constructors are better implemented using member initializer lists as below.

Also do you really NEED 2 bytes per color component?

Function overloading also allows you to have the setters and the getters use the same name.

Much of which is a matter of personal style.

class Color
{
    uint8_t _r;//ed
    uint8_t _g;//reen
    uint8_t _b;//lue

public:
    Color() : _r(0), _g(0), _b(0)
    {   }

    Color(uint8_t const r, uint8_t const g, uint8_t const b)
        : _r(r), _g(g), _b(b)
    {   }

    uint8_t red() const                                 { return _r; }
    uint8_t green() const                               { return _g; }
    uint8_t blue() const                                { return _b; }

    void red(uint8_t const r)                           { _r = r;   }
    void green(uint8_t const g)                         { _g = g;   }
    void blue(uint8_t const b)                          { _b = b;   }
    void setColor(uint8_t const r, uint8_t const g, uint8_t const b)
    {
        _r = r;
        _g = g;
        _b = b;
    }

    String getDominantColor() const;
};
  if (_red > _blue && _red > _green) {
    return "red";
  }
  else if (_blue > _red && _blue > _green) {
    return "green";
  }
  else if (_green > _blue && _green > _red) {
    return "green";
  }

If the predominant color is red, return "red". If the predominant color is blue, return "green". Did I miss something?