C++ supports both implicit and explicit type casting of variables. Error c2664 is displayed while developing C++ when an incompatible conversion is encountered. This can occur if a class is created and an implicit constructor conversion is attempted while an explicit keyword is in the code statement.

Type Casting in C++

Converting an expression into another is referred to as “type casting” in C++. The language supports both implicit and explicit type casting. When implicitly converting one variable into another, no operator is required prior to the variables. This is commonly done when converting compatible types (such as a short integer and regular integer) and is referred to as a standard conversion. Unfortunately, when this type of conversion is conducted, there is a loss of precision. However, explicit conversions help avoid this issue. Since C++ is strongly typed, many conversions are required to be explicit, which is where the type being cast to is included in parentheses before the variable name. Explicit casting on variables that are incompatible will result in a successful compilation, but an error message may be displayed at runtime.

Error c2664

When error c2664 is displayed, a parameter passed to a function cannot be converted to the required type. This can occur if the user attempts to implicitly convert a constructor that is marked with an explicit keyword. If a temporary object is passed to a function that takes a reference to a generic object as one of the parameters, then the reference has to be a const or the error message may be displayed. Also, if the function is called with a parameter that differs from what it is expecting, a temporary object is created using the applicable constructor and is passed to the function. This temporary object is then used to initiate the reference, which was allowed in the past. This is being phased out, and errors are now being displayed in the Microsoft C++ and C compiler.