如何导出包含私有结构向量的类
我有一个 A 类(我想导出),其中包含私有结构的向量。编译代码时出现警告 C4251( http://msdn.microsoft.com /en-us/library/esew7y1w.aspx )。为了防止出现此警告,我进行了显式实例化。在 VS2008 中,编译没有任何问题,但在 VS2010 中,出现以下错误:
错误 C2252:模板的显式实例化只能发生在命名空间范围内
(错误 C2252:http://msdn.microsoft.com/en-us/library/4ds5s2s4(v=vs.100).aspx )
有没有办法用向量导出类并保留结构是私有的吗?
class __declspec(dllexport) A
{
public:
A();
~A();
private:
struct StructData
{
unsigned int b_;
};
#if defined(WIN32) && !defined(__GNUC__)
template class __declspec(dllexport) std::allocator<StructData>; // explicit instantiation needed to prevent warning C4251
template class __declspec(dllexport) std::vector<StructData, std::allocator<StructData> >; // explicit instantiation needed to prevent warning C4251
#endif
std::vector<StructData> StructDataVector_;
};
I have a Class A ( that I would like to export) which contains a vector of a private struct. When compiling the code I have the warning C4251( http://msdn.microsoft.com/en-us/library/esew7y1w.aspx ). To prevent this warning I did an explicit instantiation. In VS2008 this compiles without any problems but in VS2010 I have following error:
error C2252: an explicit instantiation of a template can only occur at namespace scope
(error C2252: http://msdn.microsoft.com/en-us/library/4ds5s2s4(v=vs.100).aspx )
Is there any way to export the class with the vector and keeping the struct private?
class __declspec(dllexport) A
{
public:
A();
~A();
private:
struct StructData
{
unsigned int b_;
};
#if defined(WIN32) && !defined(__GNUC__)
template class __declspec(dllexport) std::allocator<StructData>; // explicit instantiation needed to prevent warning C4251
template class __declspec(dllexport) std::vector<StructData, std::allocator<StructData> >; // explicit instantiation needed to prevent warning C4251
#endif
std::vector<StructData> StructDataVector_;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类的所有成员也需要导出。您可以使用 pimpl idiom 来隐藏类的实现。
All members of a class need to be exported as well. You can use the pimpl idiom to hide the implementation of your class.