C++ definition of dllimport static data member(dllimport静态数据成员的C++定义)
问题描述
我确实有一个如下所示的类:
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;
我收到以下编译错误:
error C2491: 'MyClass::myInt' : definition of dllimport static data member not allowed
我该怎么办?
推荐答案
__declspec(dllimport)
表示当前代码正在使用实现您的类的 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.
如果您尝试为实现此类的 DLL 编写代码(从而定义成员函数和静态数据成员),那么您需要改为标记类 __declspec(dllexport)
.
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.
为此使用宏是很常见的.在构建 DLL 时,您定义一个宏 BUILDING_MYDLL
或类似的.在 MyClass
的标题中,你有:
It is common to use a macro for this. When building your DLL you define a macro BUILDING_MYDLL
or similar. In your header for MyClass
you then have:
#ifdef _MSC_VER
# ifdef BUILDING_MYDLL
# define MYCLASS_DECLSPEC __declspec(dllexport)
# else
# define MYCLASS_DECLSPEC __declspec(dllimport)
# endif
#endif
class MYCLASS_DECLSPEC MyClass
{
...
};
这意味着您可以在 DLL 和使用该 DLL 的应用程序之间共享标头.
This means that you can share the header between the DLL and the application that uses the DLL.
这篇关于dllimport静态数据成员的C++定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:dllimport静态数据成员的C++定义


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07