简体   繁体   English

__declspec(dllexport) 嵌套类

[英]__declspec(dllexport) on nested classes

Code:代码:

#ifdef BUILD_DLL
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif

class MY_API A
{
    public:
        void some_method();

    class B
    {
         public:
             void other_method();
    };
};

Do I have to add my macro ( MY_API ) to the B class?我是否必须将我的宏 ( MY_API ) 添加到 B 类?

Do I have to add my macro (MY_API) to the B class?我是否必须将我的宏 (MY_API) 添加到 B 类?

If that B class is also exported/imported (which, presumably, it is), then: Yes, you do.如果该B类也被导出/导入(大概是这样),那么:是的,你这样做。

Try the following code, where we are building the DLL and exporting the classes:尝试以下代码,我们将在其中构建 DLL 并导出类:

#define BUILD_DLL

#ifdef BUILD_DLL
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif

class MY_API A {
public:
    void some_method();

    class B {
    public:
        void other_method();
    };
};

// Dummy definitions of the exported member functions:
void MY_API A::some_method() {}
void MY_API A::B::other_method() {}

Compiling this gives the following error (MSVC, Visual Studio 2019):编译它会出现以下错误(MSVC,Visual Studio 2019):

error C2375: 'A::B::other_method': redefinition;错误 C2375:“A::B::other_method”:重新定义; different linkage不同的联系

The message disappears, and the code compiles without issue, if we simply add the MY_APP attribute to the nested class:如果我们简单地将MY_APP属性添加到嵌套类,消息就会消失,并且代码编译没有问题:

//...
    class MY_API B { // Add attribute to nested class
    //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM