LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 7

Error: definition differs from declaration in const Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

class Meter {
    double v_ = 1.5;
public:
    double value() const;
};

double Meter::value() { return v_; }

int main() {
    Meter m;
    std::cout << m.value() << '\n';
    return 0;
}
  • A v_ must be an int
  • B The definition omits const, so it does not match the declared double value() const – const is part of the member function's signature Correct
  • C value must be virtual
  • D Meter needs a constructor
Correct Answer: Option B

Write double Meter::value() const { … }. A const and a non-const version are different overloads.

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