I am having trouble using the constructors of the Struct
below. Specifically the ones with the parameter of type Color_type
.
It is my understanding that since the enum
Color_type
is defined within the struct
Color
providing something seemingly that could be accessed like Color::Color_type::red
, but this does not work when I call the constructor with such a value, e.g., Color p{Color::Color_type::red};
I am confused about the intention of having this enum
within the struct
and providing constructors with parameters of this type. How would one constructing this object have access to those type definitions?
Would a better solution be to use a scoped enum class
outside of the struct
that is named the same, such that it provides the means to call the constructor like Color p{Color::Color_type::red};
The same questions extend to the enum Transparency
as well.
This is a snippit of a grapics library provided by Bjarne Stroustrup in his book, Programming Principles and Practice Using C++ 2e
struct Color {
enum Color_type {
red=FL_RED, blue=FL_BLUE, green=FL_GREEN,
yellow=FL_YELLOW, white=FL_WHITE, black=FL_BLACK,
magenta=FL_MAGENTA, cyan=FL_CYAN, dark_red=FL_DARK_RED,
dark_green=FL_DARK_GREEN, dark_yellow=FL_DARK_YELLOW, dark_blue=FL_DARK_BLUE,
dark_magenta=FL_DARK_MAGENTA, dark_cyan=FL_DARK_CYAN
};
enum Transparency { invisible = 0, visible=255 };
Color(Color_type cc) :c(Fl_Color(cc)), v(visible) { }
Color(Color_type cc, Transparency vv) :c(Fl_Color(cc)), v(vv) { }
Color(int cc) :c(Fl_Color(cc)), v(visible) { }
Color(Transparency vv) :c(Fl_Color()), v(vv) { }
int as_int() const { return c; }
char visibility() const { return v; }
void set_visibility(Transparency vv) { v=vv; }
private:
unsigned char v; // 0 or 1 for now
Fl_Color c;
};
With help from this Comment I was able to further test and discover that
Color test{Color::red};
does indeed compile.The immediate error I was receiving was from using the
using
declaration in a class definition to inherit the constructors of a base class.One of the members of the that class was of type
Color
and it complained about using an constructor that was implicitly deleted because of an ill-formed definition.It was ill-formed because
Color::Color_type
was not defined within the scope of the class I was defining. Once I fully qualified the type with the containing namespace, I was able to compile.Both of the following initializations for the member
stc
inStriped_rectangle
will compile:Color stc{Graph_lib::Color::red};
andColor stc{Graph_lib::Color::Color_type::red};