加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 导出类时编译器错误

发布时间:2020-12-16 09:53:13 所属栏目:百科 来源:网络整理
导读:我正在使用Visual Studio 2013,我遇到了一个奇怪的问题.当我导出一个类时,它会抛出“试图引用已删除的函数”错误.但是,当未导出类时,它的行为正确. 让我举个例子… class Foo{}; // note the exportclass __declspec(dllexport) Bar{ // the following line
我正在使用Visual Studio 2013,我遇到了一个奇怪的问题.当我导出一个类时,它会抛出“试图引用已删除的函数”错误.但是,当未导出类时,它的行为正确.

让我举个例子…

class Foo
{

};

      // note the export
class __declspec(dllexport) Bar
{
    // the following line throws the error
    std::unordered_map<std::string,std::unique_ptr<Foo>> map;
};

现在,如果我删除导出,所以它看起来像以下一样按预期工作.

class Foo
{

};

// note I removed the export
class Bar
{
    // the following line now compiles without any issues
    std::unordered_map<std::string,这是一个编译器错误还是我显然缺少的其他东西?仅供参考,上述代码适用于GCC或Clang.

Error   2   error C2280: 'std::unique_ptr<Foo,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function    c:program files (x86)microsoft visual studio 12.0vcincludexmemory0 592

解决方法

从dll导出类时,编译器显式生成所有特殊成员方法(复制构造函数等,在这种情况下,否则将保持未声明).如您所见,生成的复制构造函数然后在唯一指针上生成无效副本;因此错误.

我不认为这只是一个错误,我认为它很可能是不受支持的情况的一部分.

您可以尝试在Bar类复制构造函数中显式删除,并检查编译器是否接受它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读