如何导出包含私有结构向量的类

发布于 2025-01-03 08:38:20 字数 1065 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2025-01-10 08:38:20

类的所有成员也需要导出。您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文