DLL将抽象基类导出为成员函数的参数 - C ++

时间:2018-04-30 18:05:50

标签: c++

我想知道如何导出一个DLL(使用__declspec(dllexport) - 所以,Windows,使用MSVC)和一个具有抽象基类(智能指针 - )作为参数的成员函数

目前,我收到错误C2280:std::unique_ptr<Base>...尝试引用已删除的功能。这是一个例子:

DLL:

class.h

#ifdef DLLFILE //defined in MSVC for compiling DLL, not defined for exe
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP __declspec(dllimport)
#endif /* DLLFILE */

#include <vector>
#include <memory>
#include <functional>
class DLLEXP Base
{ //Base class that provides a pure virtual interface to a function that could have a variety of implementations
public:
    virtual double fieldAtS(double s) = 0;
};

class DLLEXP Derived : public Base
{ //Some derived implementation of Base
private:
    std::function<double(double)> fcn_m;

public:
    Derived(std::function<double(double)> fcn);
    double fieldAtS(double s) override;
};

class DLLEXP Container
{ //Container class that stores implementations of Derived and determines the sum of all implementations
private:
    std::vector<std::unique_ptr<Base>> elems_m;

public:
    void add(std::unique_ptr<Base> ptr);
    double fieldAtS(double s);
};

class.cpp

#include "class.h"

#include <iostream>
#include <memory>
Derived::Derived(std::function<double(double)> fcn) : fcn_m{ fcn }
{

}

double Derived::fieldAtS(double s)
{
    return fcn_m(s);
}

void Container::add(std::unique_ptr<Base> ptr)
{
    elems_m.push_back(std::move(ptr));
}

double Container::fieldAtS(double s)
{
    double ret{ 0.0 };
    for (auto& der : elems_m)
        ret += der->fieldAtS(s);

    return ret;
}

EXE:

的main.cpp

#include "class.h"

#include <memory>
#include <iostream>

int main()
{
    std::unique_ptr<Container> mine{ std::make_unique<Container>() };

    mine->add(std::make_unique<Derived>([](double x) { return 10.0 * x; }));
    mine->add(std::make_unique<Derived>([](double x) { return x; }));
    mine->add(std::make_unique<Derived>([](double x) { return x / 10.0; }));

    std::cout << mine->fieldAtS(5.0) << std::endl;

    return 0;
}

我想我得到了这里发生的事情(检查我的理解)...即,编译器正在编译Container::add(unique_ptr<Base>)并且因为指定了dllexport,所以unique_ptr<Base>的字节码版本必须是包含在DLL中(而不是依赖它在运行时创建 - 这是怎么回事?),由于Base中的纯虚函数,编译器无法做到这一点。我是在正确的轨道上吗?

我的目标是:

  1. 实现fieldAtS的多态性 - 也就是说,我希望能够在运行时选择一些字段模型(并在适当的时候,将多个添加到Container /求和它们以获得来自所有元素的总字段)
  2. 从DLL导出这些类并在其他地方调用它们
  3. 仍然使用智能指针(在我的代码中的其他地方,我已经导出了动态创建类实例并传递(哑)指针的函数,以及在其他情况下接受哑指针和调用成员函数 - 希望避免这种情况。)
  4. 所以有几个问题:

    • 我对正在发生的事情有所了解吗?
    • 这是一个很好的设计方法,必须是(可能不是必须)dllexport
    • 我如何在这里实现目标?也就是说,如果这是一个好的设计,我该如何正确导出容器?如果没有,我还应该如何构建我的代码呢?

    编辑:构建日志/更新信息

    似乎错误引用了默认的复制构造函数:Base& Base::operator=(const Base&) = delete; ...所以,如果我{m} dllexport,我必须明确声明一个复制构造函数?

    构建日志如下:

    1>------ Rebuild All started: Project: DLL, Configuration: Release x64 ------
    1>  dllmain.cpp
    1>%BASEFOLDER%\include\class.h(18): warning C4251: 'Derived::fcn_m': class 'std::function<double (double)>' needs to have dll-interface to be used by clients of class 'Derived'
    1>  %BASEFOLDER%\include\class.h(18): note: see declaration of 'std::function<double (double)>'
    1>%BASEFOLDER%\include\class.h(28): warning C4251: 'Container::elems_m': class 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' needs to have dll-interface to be used by clients of class 'Container'
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    1>  %BASEFOLDER%\include\class.h(28): note: see declaration of 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>'
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xutility(2316): error C2280: 'std::unique_ptr<Base,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\memory(1436): note: see declaration of 'std::unique_ptr<Base,std::default_delete<_Ty>>::operator ='
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xutility(2335): note: see reference to function template instantiation '_OutIt std::_Copy_unchecked1<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_General_ptr_iterator_tag)' being compiled
    1>          with
    1>          [
    1>              _OutIt=std::unique_ptr<Base,std::default_delete<Base>> *,
    1>              _InIt=std::unique_ptr<Base,std::default_delete<Base>> *
    1>          ]
    1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(997): note: see reference to function template instantiation '_OutIt *std::_Copy_unchecked<std::unique_ptr<Base,std::default_delete<_Ty>>*,std::unique_ptr<_Ty,std::default_delete<_Ty>>*>(_InIt,_InIt,_OutIt)' being compiled
    1>          with
    1>          [
    1>              _OutIt=std::unique_ptr<Base,std::default_delete<Base>> *,
    1>              _Ty=Base,
    1>              _InIt=std::unique_ptr<Base,std::default_delete<Base>> *
    1>          ]
    1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(980): note: while compiling class template member function 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::operator =(const std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &)'
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    1>  %BASEFOLDER%\include\class.h(33): note: see reference to function template instantiation 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::operator =(const std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &)' being compiled
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    1>  %BASEFOLDER%\include\class.h(28): note: see reference to class template instantiation 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
    1>          with
    1>          [
    1>              _Ty=Base
    1>          ]
    2>------ Rebuild All started: Project: ClassExport, Configuration: Release x64 ------
    2>  main.cpp
    2>%BASEFOLDER%\include\class.h(18): warning C4251: 'Derived::fcn_m': class 'std::function<double (double)>' needs to have dll-interface to be used by clients of class 'Derived'
    2>  %BASEFOLDER%\include\class.h(18): note: see declaration of 'std::function<double (double)>'
    2>%BASEFOLDER%\include\class.h(28): warning C4251: 'Container::elems_m': class 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' needs to have dll-interface to be used by clients of class 'Container'
    2>          with
    2>          [
    2>              _Ty=Base
    2>          ]
    2>  %BASEFOLDER%\include\class.h(28): note: see declaration of 'std::vector<std::unique_ptr<Base,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>'
    2>          with
    2>          [
    2>              _Ty=Base
    2>          ]
    2>LINK : fatal error LNK1181: cannot open input file 'class.lib'
    ========== Rebuild All: 0 succeeded, 2 failed, 0 skipped ==========
    

1 个答案:

答案 0 :(得分:0)

我不是100%确定我的解释是正确的,但我相信原因是不同的。注意问题是在导出类时,而不是导入它。如果你删除了dllexport,一切都会顺利,但你会遇到链接器问题。

原因是您告诉编译器导出所有类函数,并且显然包括默认函数(默认构造函数,默认复制构造函数等)。这里的问题是operator=和复制构造函数,因为它们不能存在于您的类中,因为std::unique_ptr<Base>不可复制。解决方案是删除它们,如下所示:

class DLLEXP Container
{
public:
    Container();
    Container(const Container&) = delete;
    Container& operator=(const Container&) = delete;
}

在普通代码(没有DLL)中,只要尝试使用复制构造函数,就会遇到同样的问题。但在这种情况下,你会很早就得到错误。

另请注意:

  • 编译器警告你所有的memebers等应该通过dllexported,只有在你知道自己在做什么时才会忽略它
  • https://msdn.microsoft.com/en-us/library/81h27t8c.aspx?f=255&MSPPError=-2147217396描述了类
  • 的导入/导出行为
  • 导出模板和STL类是正确的,应该避免,在较旧的VS版本上,您可能会发现非常微妙的运行时错误
  • 如果没有任何变化,DLL中的模板没有字节代码。您必须使用dllexport导出专用于具体类型的模板,或者提供包括实现在内的完整标题。