Оператор = это неоднозначный (C ++)
Вопрос
У меня есть следующее в цикле для цикла, и компилятор говорит «Оператор = неоднозначен». Не уверен, как решить эту проблему, может кто-нибудь помочь?
rootelement = document->getDocumentElement();
boost::interprocess::unique_ptr<DOMNodeIterator, release_deleter> itera (document->createNodeIterator(rootelement, DOMNodeFilter::SHOW_ALL, NULL, true));
for(boost::interprocess::unique_ptr<DOMNode, release_deleter> current (itera->nextNode()); current != 0; current = boost::interprocess::unique_ptr<DOMNode, release_deleter> (itera->nextNode())) // Last assignment on current is ambiguous
Полная ошибка:
*
\XMLDocument.cpp(193) : error C2593: 'operator =' is ambiguous
c:\Program Files\boost\boost_1_44\boost\interprocess\smart_ptr\unique_ptr.hpp(249): could be 'boost::interprocess::unique_ptr<T,D> &boost::interprocess::unique_ptr<T,D>::operator =(int boost::interprocess::unique_ptr<T,D>::nat::* )'
with
[
T=xercesc_3_1::DOMNode,
D=release_deleter
]
unique_ptr.hpp(211): or 'boost::interprocess::unique_ptr<T,D> &boost::interprocess::unique_ptr<T,D>::operator =(boost::interprocess::rv<boost::interprocess::unique_ptr<T,D>> &)'
with
[
T=xercesc_3_1::DOMNode,
D=release_deleter
]
while trying to match the argument list '(boost::interprocess::unique_ptr<T,D>, boost::interprocess::unique_ptr<T,D>)'
with
[
T=xercesc_3_1::DOMNode,
D=release_deleter
]
and
[
T=xercesc_3_1::DOMNode,
D=release_deleter
]
\XMLDocument.cpp(193) : see reference to class template instantiation 'boost::interprocess::detail::unique_ptr_error<T>' being compiled
with
[
T=boost::interprocess::unique_ptr<xercesc_3_1::DOMNode,release_deleter>
]
XMLDocument.cpp(193) : see reference to class template instantiation 'boost::interprocess::detail::unique_ptr_error<T>' being compiled
with
[
T=xercesc_3_1::DOMNode *
]
\XMLDocument.cpp(193) : see reference to class template instantiation 'boost::interprocess::detail::unique_ptr_error<T>' being compiled
with
[
T=xercesc_3_1::DOMNode *
]
XMLDocument.cpp(192) : see reference to class template instantiation 'boost::int
erprocess::detail::unique_ptr_error<T>' being compiled
with
[
T=xercesc_3_1::DOMNodeIterator *
]
*
Решение
Как Стефайн говорит, unique_ptr
сохраняйте уникальность, если вы явно не перемещаете их, не назначаете им rvalue. Обычно ваш код будет в порядке, но поскольку вы притворяетесь RValues, вам нужно будет перемещать его прямо.
Я никогда не использовал boost::interprocess::unique_ptr
, но похоже, вы хотите это:
namespace bi = boost::interprocess; // do these please!
typedef bi::unique_ptr<DOMNode, release_deleter> node_ptr;
typedef bi::unique_ptr<DOMNodeIterator, release_deleter> iterator_ptr;
rootelement = document->getDocumentElement();
iterator_ptr itera(document->createNodeIterator(rootelement,
DOMNodeFilter::SHOW_ALL, NULL, true));
for (node_ptr current(itera->nextNode()); current != 0;
current = bi::move(node_ptr(itera->nextNode())))
Проще может быть:
for (node_ptr current(itera->nextNode()); current != 0;
current.reset(itera->nextNode()))
Другие советы
Я думаю, что STD :: Unique_ptr может быть назначен только при вызове STD :: Move (). Это то, как он явно теряет право собственности на основной объект.
std ::unique_ptr<T> upOldT = new T ;
std ::unique_ptr<T> pT = std ::move(upOldT) ;
Как отметил GMan, C ++ 0x еще не текущий стандарт C ++, поэтому он должен быть повышение :: Intercopess :: Unique_ptr ...
Я не уверен, и ничего не пробовал, но вы можете попробовать:
current = itera->nextNode()
Тогда это займет только одну из неясностей.