This documentation is archived and is not being maintained.

Compiler Error C2664

'function' : cannot convert parameter number from 'type1' to 'type2'

A parameter cannot be converted to the required type. This might happen if you create an instance of a class and attempt an implicit conversion on a constructor marked with the explicit keyword.

If a temporary object is passed to a function that takes a reference to an object as a parameter, that reference must be a const reference.

If the function is called with a parameter that is not of the type that the function expects, a temporary object is created using the appropriate constructor. This temporary object is then passed to the function. In this case, the temporary object is used to initialize the reference. In previous versions of the language, all references could be initialized by temporary objects. This behavior is now being phased out, hence the error given by the Microsoft C/C++ compiler.

The code below demonstrates this error by calling Test with a string literal. Because the parameter is an szString reference, an object must be created by the appropriate constructor. The result is a temporary object that cannot be used to initialize the reference.

Example 1

// C2664a.cpp
class A {} a;
func( int, A );
int main()
   func( 1, 1 );  // C2664, no conversion from int to A

Example 2

// C2664b.cpp
#include <iostream.h>
#include <string.h>

class szString
   int slen;
   char *str;

   szString(const char *);
   int len() const { return slen; }

void Test(szString &a) { cout << a.len();}

szString::szString(const char * newstr)
   str = new char[slen + 1];
   strcpy(str, newstr);

int main()
   Test("hello");   // C2664 expected

Possible solutions

  • Recheck the prototype for the given function and correct the argument noted in the error message.
  • Supply an explicit conversion if necessary.