代码:
#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();
};
};是否必须将宏(MY_API)添加到B类中?
发布于 2021-11-02 18:03:42
,我必须将宏(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 2019):
错误C2375:'A::B::other_method':重新定义;不同的链接
如果我们简单地将MY_APP属性添加到嵌套类中,消息就会消失,代码将不受问题地编译:
//...
class MY_API B { // Add attribute to nested class
//...https://stackoverflow.com/questions/69814706
复制相似问题