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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ Standard Edition, version 6.0

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

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kbCompiler kbinfo KB815647 kbAudDeveloper