C++ dllimport静态数据成员的定义

发布于 2024-09-14 04:21:00 字数 343 浏览 2 评论 0原文

我确实有一个如下所示的类:

//.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分開簡單 2024-09-21 04:21:00

__declspec(dllimport) 表示当前代码正在使用实现您的类的 DLL。因此,成员函数和静态数据成员是在 DLL 中定义的,在程序中再次定义它们是错误的。

如果您尝试为实现此类的 DLL 编写代码(从而定义成员函数和静态数据成员),那么您需要标记类 __declspec(dllexport)

为此通常使用宏。构建 DLL 时,您可以定义宏 BUILDING_MYDLL 或类似宏。在 MyClass 的标头中,您可以看到:

    #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 的应用程序之间共享标头。

__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 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
    {
        ...
    };

This means that you can share the header between the DLL and the application that uses the DLL.

情话难免假 2024-09-21 04:21:00

来自 MSDN 文档

当你声明一个类 dllimport 时,
它的所有成员函数和静态
数据成员被导入。与
dllimport 和 dllexport 的行为
非类类型,静态数据成员
无法在中指定定义
与 dllimport 相同的程序
类已定义。

希望有帮助..

From MSDN Documentation,

When you declare a class dllimport,
all its member functions and static
data members are imported. Unlike the
behavior of dllimport and dllexport on
nonclass types, static data members
cannot specify a definition in the
same program in which a dllimport
class is defined
.

Hope it helps..

幸福还没到 2024-09-21 04:21:00

如果您要导入一个类,那么您将导入它及其所有成员,因此不可能在“客户端”定义任何类成员。应该使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文