LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 10

Error: using a private nested class outside Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

class List {
    struct Node {
        int value;
        Node* next;
    };
public:
    int size() const { return 0; }
};

int main() {
    List::Node n{5, nullptr};
    std::cout << n.value << '\n';
    return 0;
}
  • A Node must have a constructor
  • B Node is declared in the private section of List, so code outside List cannot name it Correct
  • C next must be a reference
  • D List must be a struct
Correct Answer: Option B

Private nested classes are implementation details. Make Node public if users really need it.

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