LearnFrenzy
Sign in Take a free test

Operator Overloading · Find the Error · Question 2

Error: overloading an operator for two ints Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

int operator+(int a, int b) { return a - b; }

int main() {
    std::cout << 5 + 3 << '\n';
    return 0;
}
  • A The function needs to be inline
  • B An overloaded operator needs at least one operand of class or enumeration type; built-in int + int cannot be redefined Correct
  • C It must return long
  • D operator+ must be a member
Correct Answer: Option B

The language protects the meaning of operations on built-in types.

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