2015-07-10 36 views
2

说我有一类本身引用的STL容器:导出包含在一个DLL接口相同类别的STL类成员

class __declspec(dllexport) Widget { 
protected: 
    std::vector<Widget *> children; 
} 

如果我有类定义之前说intvector相反,我可以有以下几种:

template class __declspec(dllexport) std::vector<int>; 

这应该会工作。但是如果这个班级没有定义,我该怎么做?向前声明该类:

class Widget; 
template class __declspec(dllexport) std::vector<Widget *>; 

不会摆脱我从MSVC得到的警告;

warning C4251: 'Widget::children' : class 'std::vector<Widget *,std::allocator<_Ty>>' needs to have dll-interface to be used by clients of class 'Widget'

我相信警告本身的信息是有点相关,但我不知道该怎么办才好。

+0

我会尝试'模板类__declspec(dllexport)std ::向量>;' –

+0

类__declspec(dllexport)Widget作为前向声明是发布的吗? – marom

回答

3

取自here,这些错误的家族本质上是噪声;

C4251基本上是噪声,并且可以被沉默
- 斯蒂芬T. Lavavej(项目建立在微软的C++库的维护者的之一)。

只要编译器选项在整个项目中保持一致,只要将此警告消除就可以了。

作为一个更全面替代你可以看看pimpl pattern并从类定义的std::vector,因此它不会需要从DLL导出。

class __declspec(dllexport) Widget { 
protected: 
    struct Pimpl; 
    Pimpl* pimpl_; 
} 

// in the cpp compiled into the dll 
struct Widget::Pimpl { 
    // ... 
    std::vector<Widget*> children; 
    // ... 
} 

MSDN has a nice article on this作为介绍到更新的C++ 11个特征的一部分。

通常,在dll interface中不使用std类更容易,尤其是在需要互操作性的情况下。

0

警告本身确实相关。其中提到的头文件安装在与我正在处理的目录不同的目录中,所以我的更改没有看到。修正警告的正确方法是:

class Widget; 
template class __declspec(dllexport) std::vector<Widget *>; 

在类定义之前。

相关问题