LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Find the Error · Question 3

Error: an enum class enumerator used without its scope Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

enum class Light { red, yellow, green };

int main() {
    Light l = green;
    std::cout << (l == Light::green) << '\n';
    return 0;
}
  • A Light must be an int
  • B Enumerators of an enum class are scoped: write Light::green (or using enum Light; in C++20) Correct
  • C green is a keyword
  • D l must be const
Correct Answer: Option B

Scoping prevents clashes such as two enums both having red.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links