C++ dllimport静态数据成员的定义
我确实有一个如下所示的类:
//.h file
class __declspec(dllimport) MyClass
{
public:
//stuff
private:
static int myInt;
};
// .cpp file
int MyClass::myInt = 0;
我收到以下编译错误:
error C2491: 'MyClass::myInt' : definition of dllimport static data member not allowed
我应该做什么?
I do have a class which looks like below:
//.h file
class __declspec(dllimport) MyClass
{
public:
//stuff
private:
static int myInt;
};
// .cpp file
int MyClass::myInt = 0;
I get the following compile error:
error C2491: 'MyClass::myInt' : definition of dllimport static data member not allowed
what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
__declspec(dllimport)
表示当前代码正在使用实现您的类的 DLL。因此,成员函数和静态数据成员是在 DLL 中定义的,在程序中再次定义它们是错误的。如果您尝试为实现此类的 DLL 编写代码(从而定义成员函数和静态数据成员),那么您需要标记类
__declspec(dllexport)
。为此通常使用宏。构建 DLL 时,您可以定义宏
BUILDING_MYDLL
或类似宏。在MyClass
的标头中,您可以看到:这意味着您可以在 DLL 和使用该 DLL 的应用程序之间共享标头。
__declspec(dllimport)
means that the current code is using the DLL that implements your class. The member functions and static data members are thus defined in the DLL, and defining them again in your program is an error.If you are trying to write the code for the DLL that implements this class (and thus defines the member functions and static data members) then you need to mark the class
__declspec(dllexport)
instead.It is common to use a macro for this. When building your DLL you define a macro
BUILDING_MYDLL
or similar. In your header forMyClass
you then have:This means that you can share the header between the DLL and the application that uses the DLL.
来自 MSDN 文档,
希望有帮助..
From MSDN Documentation,
Hope it helps..
如果您要导入一个类,那么您将导入它及其所有成员,因此不可能在“客户端”定义任何类成员。应该使用 dllexport 关键字代表实现 dll
if you are importing a class you are importing it with all it members so it is impossible to define any class member on the "client side". dllexport keyword should be used on behalf of implementation dll