tag:blogger.com,1999:blog-4912226416400969049.post2608030365516494605..comments2024-03-04T23:53:54.164+00:00Comments on My Public Notepad: Prefer compile-time to run-time value range checks: use enumerationsBojan Komazechttp://www.blogger.com/profile/05437236807206825832noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4912226416400969049.post-56914871775183427462012-01-19T20:09:26.052+00:002012-01-19T20:09:26.052+00:00I wrote at the end of the article that integers mu...I wrote at the end of the article that integers must be explicitly casted to enum type which you didn't do and therefore you got compiler error. You can use <b>static_cast</b> operator to achieve that: <b>SetLevel(static_cast<Level>(0))</b>. C cast would do the job as well (but avoid C casts in C++ code): <b>SetLevel((Level)0)</b>. Of course, the best way is to use argument of the enum type: <b>SetLevel(ZERO)</b>. CheersBojan Komazechttps://www.blogger.com/profile/05437236807206825832noreply@blogger.comtag:blogger.com,1999:blog-4912226416400969049.post-78205985346308085362012-01-19T19:14:51.973+00:002012-01-19T19:14:51.973+00:00I don't know which compiler you're using b...I don't know which compiler you're using but mine says:<br /><br /> g++ level.cpp -o level<br />level.cpp: In function ‘int main()’:<br />level.cpp:18: error: invalid conversion from ‘int’ to ‘Level’<br />level.cpp:18: error: initializing argument 1 of ‘void SetLevel(Level)’<br />level.cpp:19: error: invalid conversion from ‘int’ to ‘Level’<br />level.cpp:19: error: initializing argument 1 of ‘void SetLevel(Level)’<br />level.cpp:20: error: invalid conversion from ‘int’ to ‘Level’<br />level.cpp:20: error: initializing argument 1 of ‘void SetLevel(Level)’<br /><br />If I do:<br /><br /> SetLevel(0); // Line 18<br /> SetLevel(-1);<br /> SetLevel(2);Stefan Naewehttps://www.blogger.com/profile/13660645633972004538noreply@blogger.com