我在for循环中有以下内容,编译器说'operator = is ambiguous'。不知道如何解决这个问题,任何人都可以帮忙吗?
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 *
]
*
答案 0 :(得分:3)
与Stephane一样,unique_ptr
保持唯一性,除非您明确移动它们,或为它们分配右值。通常情况下,你的代码会没问题,但是因为你伪造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()))
答案 1 :(得分:1)
我认为只能通过调用std :: move()来分配std :: unique_ptr。 这是它明显失去底层对象所有权的方式。
std ::unique_ptr<T> upOldT = new T ;
std ::unique_ptr<T> pT = std ::move(upOldT) ;
正如GMan所说,C ++ 0x还不是当前的C ++标准,所以它应该是 boost :: interprocess :: unique_ptr ...
答案 2 :(得分:0)
我不确定,也没有尝试过,但你可以尝试:
current = itera->nextNode()
然后它只会带来一个含糊之处。