-4

I have an LEDControl class which sets the color of an LED using a method. Many classes use this LEDControl class to pass it some colors to assign to an LED. Therefore, I wanted to define the colors somewhere as a constant.

I thought I'd make a struct in my LEDControl class called Color, since I really only need to access the members directly and never anything else:

struct Color{
    bool r;
    bool g;
    bool b;
};

I then added a #define in the .cpp

#define RED Color{true, false, false};

But this didn't work; it is not declared in the scope of any other class.

What can I do to store a set of colors somewhere in my program so that every class using the LEDControl can use keywords or variable names like RED and GREEN?

Zimano
  • 1,870
  • 2
  • 23
  • 41

2 Answers2

4

Instead of using macros, provide inline constexpr instances of your struct:

namespace color
{
    inline constexpr Color red{true, false, false};
    inline constexpr Color green{false, true, false};
    inline constexpr Color blue{false, false, true};
}

You can provide these in an header file so that they will be accessible from any file including it.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
0

put the definition in a header. Please avoid #define
it could look like:

static constexpr Color RED{true, false, false};
sp2danny
  • 7,488
  • 3
  • 31
  • 53