2010-10-27 65 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() 

然後它會只有含糊不清的一個。