LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 8

Error: designated initializers out of order Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

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

main.cpp
#include <iostream>

struct Window {
    int width = 800;
    int height = 600;
    bool fullscreen = false;
};

int main() {
    Window w{.height = 720, .width = 1280};
    std::cout << w.width << 'x' << w.height << '\n';
    return 0;
}
  • A Window needs a constructor
  • B C++20 designated initializers must follow the order in which the members are declared: {.width = 1280, .height = 720} Correct
  • C bool members are not allowed
  • D Designators need parentheses
Correct Answer: Option B

Skipping members is allowed (fullscreen keeps its default); reordering is not, unlike C.

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