【问题标题】:Compiler error when exporting class导出类时出现编译器错误
【发布时间】:2014-07-06 19:14:37
【问题描述】:

我正在使用 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, std::unique_ptr<Foo>> map;
};

现在,这是编译器错误还是我明显遗漏的其他东西?仅供参考,以上代码适用于 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.0\vc\include\xmemory0 592 

【问题讨论】:

  • 这似乎不是完整的错误消息。请编辑您的问题以包含 completeunedited 错误日志。
  • 我现在已经粘贴了完整的错误。

标签: c++ visual-c++ c++11


【解决方案1】:

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

我不认为这只是一个错误,我认为它很可能是不受支持的场景的一部分。

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

【讨论】:

  • 谢谢,编译器需要一个显式的复制和复制赋值构造函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 2015-09-02
  • 2018-09-26
相关资源
最近更新 更多