unique_ptr
silinmiş kopya kurucu için VS2013 derleme Basit bir örnekNasıl Visual Studio
class __declspec(dllexport) A
{
public:
vector<unique_ptr<int>> v;
};
Hata ile dll ihraç sınıfta benzersiz işaretçiler vektör kullanmak. __declspec(dllexport)
kaldırırsam, sorun değil. Sadece unique_ptr<int> v
kullanırsam, bu da iyi. Bu bir derleyici hatası mı? Etrafında çalışmak için herhangi bir yolu? Teşekkürler.
Sen derleyici hatası veren aşağıdaki komple kod
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class __declspec(dllexport) A
{
public:
vector<unique_ptr<int>> v;
};
int main()
{
cout << "Hello World" << endl;
}
ile http://webcompiler.cloudapp.net/ üzerinde deneyebilirsiniz: örtük ilan kopya kurucusu tanımlamak için O __declspec(dllexport)
ekleyerek görünür
Compiled with /EHsc /nologo /W4 /c
main.cpp
main.cpp(9): warning C4251: 'A::v': class 'std::vector<std::unique_ptr<int,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 'A'
with
[
_Ty=int
]
c:\tools_root\cl\inc\xutility(2144): error C2280: 'std::unique_ptr<int,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
with
[
_Ty=int
]
c:\tools_root\cl\inc\memory(1430): note: see declaration of 'std::unique_ptr<int,std::default_delete<_Ty>>::operator ='
with
[
_Ty=int
]
c:\tools_root\cl\inc\xutility(2165): note: see reference to function template instantiation '_OutIt std::_Copy_impl<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
with
[
_OutIt=std::unique_ptr<int,std::default_delete<int>> *,
_InIt=std::unique_ptr<int,std::default_delete<int>> *
]
c:\tools_root\cl\inc\vector(973): note: see reference to function template instantiation '_OutIt std::_Copy_impl<std::unique_ptr<int,std::default_delete<_Ty>>,std::unique_ptr<_Ty,std::default_delete<_Ty>>*>(_InIt,_InIt,_OutIt)' being compiled
with
[
_OutIt=std::unique_ptr<int,std::default_delete<int>> *,
_Ty=int,
_InIt=std::unique_ptr<int,std::default_delete<int>> *
]
c:\tools_root\cl\inc\vector(956): note: while compiling class template member function 'std::vector<std::unique_ptr<int,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>>>> &)'
with
[
_Ty=int
]
main.cpp(10): note: see reference to function template instantiation 'std::vector<std::unique_ptr<int,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
with
[
_Ty=int
]
main.cpp(9): note: see reference to class template instantiation 'std::vector<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
with
[
_Ty=int
]
Sorun tanımlama, derleyici çıktısı, MCVE - yalnızca tüm SO soruları böyle görünüyorsa! – Angew
Önerebilirsem: Bunu yapmayın. Bu sınıfı sadece bir bağlantı yerine bir DLL'ye koymanın avantajı olarak ne düşünüyorsunuz? DLL'leri (özellikle, yeniden kullanılabilirlik) ana avantajları artık sınıfları dışa aktarırken geçerli değildir. –
Ben ile aynı fikirdeyim. DLL'lerin avantajlarına sahip olmak istiyorsanız, yalnızca bir C API'sini (veya CppCon 2014'te sunulan klasik "kum saati arayüzüne" (https://www.youtube.com/watch?v=PVYdHDm0q6Y) bakın.)). Aksi takdirde, sadece statik bağlantı (.lib/.a dosyaları) kullanın. DLL'leri dağıtırken/yeniden kullanırken C++ ABI sorunları ile uğraşmak, avantajlarının karşılaştırmalı olarak soluklaştığı bir kabus. –