2010-10-27 66 views
1

我在for循环中有以下内容,编译器说'运算符=不明确'。不知道如何解决这个问题,任何人都可以帮忙吗?运算符=不明确(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 * 
     ] 

*

回答

3

像斯特凡说:unique_ptr的保持唯一性,除非你明确地移动它们,或右值分配给他们。通常情况下,你的代码会很好,但由于你伪造了右值,你需要明确地移动它。

我从来没有用过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())) 
+0

@这是一个很好的解决方案,但我认为在每次迭代时智能指针会删除当前的问题,这会在release()函数内引发异常......也许我需要一个shared_ptr? ?? – 2010-10-27 10:08:13

1

我觉得一个std ::的unique_ptr只能打电话到std分配::移动()。 这是它明确失去底层对象所有权的方式。

std ::unique_ptr<T> upOldT = new T ; 
std ::unique_ptr<T> pT = std ::move(upOldT) ; 

由于GMAN曾说过,C++ 0x中还不是当前的C++标准,所以它应该是 的boost ::进程间:: ...的unique_ptr

+0

'的boost ::进程间:: unique_ptr',不'STD: :'。但你是对的。 – GManNickG 2010-10-27 09:40:40

+0

所以我需要创建一个新的unique_ptr并将其移入它,所有内部的循环声明? – 2010-10-27 09:46:50

+1

@Tony:通常(使用'std :: unique_ptr')你有什么好。但是,由于你的编译器不支持它,而Boost伪造它,你需要通过明确地移动它来协助Boost。 – GManNickG 2010-10-27 09:49:03

0

我不知道,和没有尝试过任何东西,但你可以尝试:

current = itera->nextNode() 

然后它会只有含糊不清的一个。