LearnFrenzy
Sign in Take a free test

Static and Friend Members · Find the Error · Question 9

Error: using a static member without its class name Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

class Settings {
public:
    static inline int volume = 40;
};

void louder() {
    volume += 10;
}

int main() {
    louder();
    std::cout << Settings::volume << '\n';
    return 0;
}
  • A volume must be const
  • B Outside the class, a static member must be named with its class: Settings::volume; plain volume is undeclared in louder() Correct
  • C louder must be a friend
  • D static inline is not allowed
Correct Answer: Option B

Only code inside the class (its member functions) can use the unqualified name.

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