1

我在 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 *
        ]

*

4

3 回答 3

3

就像 Stephane 所说,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()))
于 2010-10-27T09:48:11.157 回答
1

我认为 std::unique_ptr 只能通过调用 std::move() 来分配。这是它明确失去底层对象所有权的方式。

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

正如 GMan 所说,C++0x 还不是当前的 C++ 标准,所以它应该是 boost::interprocess::unique_ptr ...

于 2010-10-27T09:38:48.750 回答
0

我不确定,也没有尝试过任何东西,但您可以尝试:

current = itera->nextNode()

那么它只需要其中一种歧义。

于 2010-10-27T09:40:47.173 回答