gpt4 book ai didi

c++ - 导出类时编译器错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:44 27 4
gpt4 key购买 nike

我正在使用 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 

最佳答案

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

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

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

关于c++ - 导出类时编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24599414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com