LearnFrenzy
Sign in Take a free test

References: lvalue and rvalue · Find the Error · Question 8

Error: binding a reference of the wrong type Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

int main() {
    double rate = 7.5;
    int& r = rate;
    std::cout << r << '\n';
    return 0;
}
  • A rate must be float
  • B An int& cannot bind to a double; the types must match (const int& would bind to a converted temporary copy instead) Correct
  • C References cannot hold decimals
  • D r must be declared first
Correct Answer: Option B

const int& r = rate; compiles but refers to a temporary int with value 7, not to rate – often a surprise.

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