vs2010 build error with BOOST_MOVABLE_BUT_NOT_COPYABLE
以下是简化代码:
-------------- class foo -----------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | class foo : private boost::noncopyable //--- mark 1--- { BOOST_MOVABLE_BUT_NOT_COPYABLE(foo) //--- mark 2--- public: /** * Move constructor. */ foo(BOOST_RV_REF(foo) other); /** * Move-assignment. */ foo& operator=(BOOST_RV_REF(foo) other); /** * Releases */ ~foo(); ...... private: fool_impl* m_pimpl; } foo::foo(foo_impl* pimpl) :m_pimpl(pimpl) {} foo::foo( BOOST_RV_REF(foo) other) : m_pimpl(other.m_pimpl) { other.m_pimpl = NULL; } foo& foo::operator=( BOOST_RV_REF(foo) other) { if (&other != this) { delete m_pimpl; m_pimpl = other.m_pimpl; other.m_pimpl = NULL; } return *this; } foo::~foo() { delete m_pimpl; } |
---------------班吧-----------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class bar { public: explicit bar(BOOST_RV_REF(foo) other); ...... private: foo m_foo; }; bar::bar(BOOST_RV_REF(foo) other) : m_foo(other) //--- mark 3---- {} |
使用VS2010构建时,标记3报告错误:
1 2 3 | error C2248: 'foo' : cannot access private member declared in class 'foo' 1> mark 1: see declaration of 'foo' 1> mark 2: see declaration of 'foo' |
我试图修改一些代码,但没用。
有人可以提出任何建议吗? 谢谢。
@Piotr S.
谢谢你的回答,我试过,错误消失了,但又出现了另一个错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class test { public: explicit test( BOOST_RV_REF(foo) other); private: boost::shared_ptr m_class; }; test::test(BOOST_RV_REF(foo) other) { m_class = boost::make_shared(boost::ref(other)); } |
错误LNK2019:未解析的外部符号"private:__ cdecl foo(class foo const&)"在函数"class boost :: shared_ptr __cdecl boost :: make_shared const>(类boost :: reference_wrapper const&&)"中引用
你能提出什么建议吗? 谢谢!
您需要使用
1 2 3 4 | bar::bar(BOOST_RV_REF(foo) other) : m_foo(boost::move(other)) // move it ! {} |
否则,编译器正在尝试访问复制构造函数