繁体   English   中英

不完整类型的C ++导出模板实现

[英]C++ export template implementation of incomplete type

我有以下情况:

header.h:

class A
{
    public:

    class B; // incomplete type

private:
    // is never used outside of source file for A
    std::vector<B> vector_of_bs; // template
}

source1.cpp:

class A::B {}; // defined here

一切都很好,直到这里。 现在,如果我想在其他地方使用class A ,则无法使用:

source2.cpp :使用类A且不与

向量(721):错误C2036:'A :: B *':未知大小

同时编译类模板成员函数'std :: vector <_Ty>&std :: vector <_Ty> :: operator =(const std :: vector <_Ty>&)'

在VS2010中。 如何链接到source1.o中std :: vector的模板专业化?

我还需要将其与declspec(_dllimport)一起使用...

标准库容器不能使用不完整的类型实例化 这样做是不确定的行为,在您的情况下会产生明显的错误。 其他实现将静默地接受您的代码。

为了解决这个问题,boost提供了标准库容器的副本,实际上可以使用不完整的类型实例化这些库 您可以用boost替换std::vector来修复代码:

#include <boost/container/vector.hpp>

class A
{
 public: 
    class B; // incomplete type

 private:
    boost::vector<B> vector_of_bs;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM