我必须将我的宏 (MY_API) 添加到 B 类吗?
如果B
该类也被导出/导入(大概是这样),那么:是的,你这样做了。
试试下面的代码,我们在其中构建 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() {}
编译它会产生以下错误(MSVC,Visual Studio 2019):
错误 C2375:“A::B::other_method”:重新定义;不同的联动
MY_APP
如果我们简单地将属性添加到嵌套类中,消息就会消失,并且代码编译不会出现问题:
//...
class MY_API B { // Add attribute to nested class
//...