RAD Studio
ContentsIndex
PreviousUpNext
E2034: Cannot convert 'type1' to 'type2' (C++)

An assignment, initialization, or expression requires the specified type conversion to be performed, but the conversion is not legal. 

In C++, the compiler will convert one function pointer to another only if the signature for the functions are the same. Signature refers to the arguments and return type of the function. For example:

myex( int );
typedef int ( *ffp )( float );
test()
{
   ffp fp = myex; //error
}

Seeing that myex takes an int for its argument, and fp is a pointer to a function which takes a float as argument, the compiler will not convert it for you. 

In cases where this is what is intended, performing a typecast is necessary:

myex( int );
typedef int ( *ffp )( float );
test()
{
   ffp fp = (ffp)myex;  //ok
}
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!