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

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:
Propriedades

ID do Artigo: 815647 - Última Revisão: 14 de nov de 2007 - Revisão: 1

Comentários