LearnFrenzy
Sign in Take a free test

Static and Friend Members · Find the Error · Question 1

Error: a static function reading a non-static member Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

class Order {
    double amount_ = 499;
public:
    static double with_tax() { return amount_ * 1.18; }
};

int main() {
    std::cout << Order::with_tax() << '\n';
    return 0;
}
  • A 1.18 must be a constant
  • B with_tax is static, so there is no object whose amount_ it could read; make it a non-static member or pass an Order Correct
  • C amount_ must be public
  • D static functions cannot return double
Correct Answer: Option B

Non-static members belong to objects; a static member function has none.

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