Microsoft KB Archive/815647

= You receive a Compiler C2491 error message when you try to define data members as dllimport functions =

Article ID: 815647

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-





SUMMARY
You cannot apply the __declspec(dllimport) keyword to implement a function. For example, when you try to define data members (including static data members and functions) as dllimport functions, you receive the following Compiler C2491 error message:

'identifier' : definition of dllimport function not allowed



MORE INFORMATION
You can only apply the __declspec(dllimport) keyword to declarations. You cannot apply the __declspec(dllimport) keyword to implement functions. The purpose of this keyword is to declare the implementation of a function by a DLL. Similarly, if you apply the __declspec(dllimport) keyword to a data member, you receive the initial data from a DLL. Therefore, you cannot assign a value in your code initially.

You receive the Compiler C2491 error message when you try to compile the following code: // function definition void __declspec(dllimport) funcB {  // error C2491: 'funcB' : definition of dllimport function not allowed } This behavior occurs because you defined the function implementation as dllimport. To avoid this compiler error, do not define the function, but instead declare the function as follows: // function declaration void __declspec(dllimport) funcB;   // ok

int main { } Similarly, you receive the Compiler C2491 error message when you try to compile the following code: //defining data member extern __declspec(dllimport) int code = 1; // error C2491: 'code' : definition of dllimport data not allowed You receive this error message because you defined the data member as dllimport. To avoid this compiler error, do not define the data member, but instead declare the data member as follows: // declaring data member extern __declspec(dllimport) int code;  // ok

