http://en.cppreference.com/w/cpp/language/pimplからいくつかのコードをコピーしました。<memory>
とmain.cpp
を修正しました。std::experimental::propagate_const<std::unique_ptr<impl>>
を修正しました。 Visual Studioのunique_ptr trouble? (VS 2015)
// widget.h
#include <memory>
class widget
{
// public members
private:
struct impl;
std::unique_ptr<impl> pImpl;
};
。
// widget.cpp
#include "widget.h"
struct widget::impl {
// implementation details
};
widget.cpp
罰金コンパイルが、main.cpp
はありません。
#include "widget.h"
int main()
{
widget w;
}
エラーメッセージ。
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1193): error C2027: use of undefined type 'widget::impl'
1> e:\***\***\***\widget.h(8): note: see declaration of 'widget::impl'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1192): note: while compiling class template member function 'void std::default_delete<_Ty>::operator()(_Ty *) noexcept const'
1> with
1> [
1> _Ty=widget::impl
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1397): note: see reference to function template instantiation 'void std::default_delete<_Ty>::operator()(_Ty *) noexcept const' being compiled
1> with
1> [
1> _Ty=widget::impl
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1227): note: see reference to class template instantiation 'std::default_delete<_Ty>' being compiled
1> with
1> [
1> _Ty=widget::impl
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1236): note: see reference to class template instantiation 'std::_Get_deleter_pointer_type<_Ty,std::default_delete<_Ty>>' being compiled
1> with
1> [
1> _Ty=widget::impl
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1279): note: see reference to class template instantiation 'std::_Unique_ptr_base<_Ty,_Dx>' being compiled
1> with
1> [
1> _Ty=widget::impl,
1> _Dx=std::default_delete<widget::impl>
1> ]
1> e:\***\***\***\***.h(10): note: see reference to class template instantiation 'std::unique_ptr<widget::impl,std::default_delete<_Ty>>' being compiled
1> with
1> [
1> _Ty=widget::impl
1> ]
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1194): error C2338: can't delete an incomplete type
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1195): warning C4150: deletion of pointer to incomplete type 'widget::impl'; no destructor called
1> e:\***\***\***\widget.h(8): note: see declaration of 'widget::impl'
このcppreferenceページの最後には、より完全な例があります。 – Cubbi