error: non-const lvalue reference to type cannot bind to a value of unrelated type

Project scenario:

The error information reported during project code compilation is as follows:

error: non-const lvalue reference to type '...' cannot bind to a value of unrelated type '...'

Problem Description:

After the project problem is simply abstracted, it is equivalent to the following problems:

Do this and compile through

 int a;
 const double& m = a;

However, an error is reported in this compilation

 int a;
 double& m = a;

Error information such as title


Cause analysis:

Because temporary variables cannot be bound to a non const reference

 double& m = a;

a is an int variable that will be implicitly converted to a double variable, so this process generates a temporary variable. Non const reference cannot bind temporary variables


Solution:

Avoid implicit conversion or add const

Read More: