C++ 警告 C4251:需要有 dll 接口供类的客户端使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11189662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning C4251: needs to have dll-interface to be used by clients of class
提问by chintan s
Possible Duplicate:
std::vector needs to have dll-interface to be used by clients of class 'X<T> warning
This is my first post in this group.
这是我在这个群里的第一篇文章。
I am creating a DLL and calling it in the main file of the application. The code compiles fine but I get the following error:
我正在创建一个 DLL 并在应用程序的主文件中调用它。代码编译正常,但出现以下错误:
warning C4251: 'PNCBaseClass::m_vAvailChannelsFromRx' : class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'PNCBaseClass'
3> with
3> [
3> _Ty=int
3> ]
My code is as follows:
我的代码如下:
#define TEST_API __declspec(dllexport)
class TEST_API PNCBaseClass
{
public:
vector<int> m_vAvailChannelsFromRx
};
I have looked up for solutions and tried and failed.
我一直在寻找解决方案并尝试过但失败了。
I do not want to disable the warning.
我不想禁用警告。
回答by Alex F
Never keep STL containers as exported class members. Client application may be compiled with different STL version than yours, with undefined runtime behavior. In your case, it is easy to replace vector<int> member with pointer vector<int>*. Initialize it in the class constructor, and release in the class destructor.
永远不要将 STL 容器作为导出的类成员。客户端应用程序可能使用与您不同的 STL 版本进行编译,并具有未定义的运行时行为。在您的情况下,很容易用指针 vector<int>* 替换 vector<int> 成员。在类构造函数中初始化,在类析构函数中释放。