代码之家  ›  专栏  ›  技术社区  ›  Tony The Lion

运算符=模棱两可(C++)

  •  1
  • Tony The Lion  · 技术社区  · 14 年前

    我在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 回复  |  直到 14 年前
        1
  •  3
  •   GManNickG    14 年前

    就像斯蒂芬说的, 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()))
    
        2
  •  1
  •   Stephane Rolland    14 年前

    我认为STD::UnQuyJPTR只能被分配给STD:: 这就是它明确地失去底层对象所有权的方式。

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

    正如GMan所说的,C++0X还不是当前的C++标准,所以应该是 boost::进程间::唯一指针…

        3
  •  0
  •   Jan    14 年前

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

    current = itera->nextNode()
    

    那就只需要其中一个模棱两可了。