Оператор = является неоднозначным (C ++) - PullRequest
1 голос
/ 27 октября 2010

У меня есть следующее в цикле for, и компилятор говорит 'operator = является неоднозначным'.Не знаете, как решить эту проблему, кто-нибудь может помочь?

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 ]

3 голосов
/ 27 октября 2010

Как говорит Стефан, 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 голос
/ 27 октября 2010

Я думаю, что 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 ...

0 голосов
/ 27 октября 2010

Я не уверен, и ничего не пробовал, но вы можете попробовать:

current = itera->nextNode()

Тогда потребуется только одна из неясностей.

...