I am following this question for iterating over an enum.
enum class COLOR
{
Blue,
Red,
Green,
Purple,
First=Blue,
Last=Purple
};
COLOR operator++( COLOR& x ) { return x = (COLOR)(((int)(x) + 1)); }
COLOR operator*(COLOR c) {return c;}
COLOR begin(COLOR r) {return COLOR::First;}
// end iterator needs to return one past the end!
COLOR end(COLOR r) {return COLOR(int(COLOR::Last) + 1);}
The problem is that in my project, there are many cpp
and hpp
files which are compiled separately. It seems the compiler needs to have direct access to implementation of operator++
. If I declare in a hpp
and then implement in cpp
file, I will face with error:
compiler warning: inline function ‘Color operator++(Color&)’ used but never defined
linker error: undefined reference to `operator++(instruction_type&)'
If I define it directly in hpp
, I will face with another error
multiple definition of ...
for operator*
, begin
, and end
in linker.