LearnFrenzy
Sign in Take a free test

Inheritance · Find the Error · Question 10

Error: inheriting from a class that is only declared Answer, explanation & discussion

  • Find the error
  • Easy
  • 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 Base;

class Derived : public Base {
public:
    void hello() const { std::cout << "hello\n"; }
};

class Base {
public:
    int id = 1;
};

int main() {
    Derived d;
    d.hello();
    return 0;
}
  • A hello must be virtual
  • B A base class must be completely defined before a class can derive from it; a forward declaration is not enough – define Base first Correct
  • C Derived must be a struct
  • D id must be private
Correct Answer: Option B

The compiler needs the base's size and members to lay out the derived class.

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