C ++ Boost Multi Index,Modify需要Functor,无法编译

时间:2012-06-26 12:18:56

标签: c++ boost multi-index

我已经设置了一个Windows C ++ Visual Studio项目(VS9)来测试Boost :: Multi_Index的使用。 正在设置容器以存储指向自定义对象FC :: ClientOrder的共享指针。对于测试,对象非常简单,只存储一个字符串和一个char变量。 Boost版本为1_46_0

我已经将Multi_Index容器配置为在共享指针上作为标识进行索引,并使用成员函数来访问两个成员变量的索引。这似乎很好,效果很好。

我遇到了modify()方法的问题。我意识到如果我想更新属于索引的任何成员变量,我必须使用modify()。我也意识到我必须首先使用find()在容器中找到FC :: ClientOrder对象,然后将迭代器传递给modify()以进行更改。

如果我通过标识,共享指针本身找到FC :: ClientOrder共享指针,一切运行正常,并且modify()很高兴。

如果我通过成员函数找到FC :: ClientOrder共享指针,clOrdID()(基本上查找它的ID上的顺序,非常常用),那么即使语法相同,modify()函数也会失败。我收到编译器错误,这似乎表明存在类型问题,但我发现很难理解错误。

modify()只能使用基于身份索引的迭代器吗?如果是这样,要通过它的clOrdID编辑订单,我将不得不进行两次查找,一次查找容器中的顺序(并获取它的spOrder变量),然后根据spOrder值创建一个迭代器。这似乎打败了能够通过clOrdID()值提取订单的目标。

我认为我误解了modify()函数的要求。现在,我可以通过使用双重查找解决这个问题,但如果有人有解决方案,我将非常感激。我对Functors很新,所以也许我误解了modify()需要什么。

在此先感谢您的帮助,我们将不胜感激。示例代码块应该遵循......

///////////////////////////////////////////////////////////////////////////////
// FILE #1: FCClientOrder.h
///////////////////////////////////////////////////////////////////////////////

#ifndef FCClientOrder_H
#define FCClientOrder_H

#include <string>                                                               // Changed to use std::string

////////////////////////////////////////////////////////////////////////////////
// ClientOrder v3: RAM definition of a market order - stripped out for testing

namespace FC
{

class ClientOrder
{
public:


    ClientOrder();                                                              // Default Ctor

    virtual ~ClientOrder();                                                     // Dtor


    //////////////////////
    // ACCESSOR FUNCTIONS

    std::string clOrdID(void) const;                                            // Get
    void clOrdID(const std::string& sClOrdID);                                  // Set

    char ordStatus(void) const;                                                 // Get
    void ordStatus(char cOrdStatus);                                            // Set


    ///////////////////
    // PUBLIC STATICS

    static void change_status_static(std::tr1::shared_ptr<FC::ClientOrder> spOrder,char cOrdStatus);    // Static method to change status

    // Functor
    struct change_status_by_sp
    {
        change_status_by_sp(char cOrdStatus)                                            // Ctor
        :   _cOrdStatus( cOrdStatus )
        {}

        void operator()(std::tr1::shared_ptr<FC::ClientOrder> spOrder)          // Functor
        { spOrder->ordStatus( _cOrdStatus ); }

    private:
        char _cOrdStatus;
    };


private:

    ////////////////////
    // PRIVATE MEMBERS

    std::string                 m_sClOrdID;                                     // Client order ID
    char                        m_cOrdStatus;                                   // Order Status

}; // end of class ClientOrder

} // end of namespace FC

#endif // FCClientOrder_H




///////////////////////////////////////////////////////////////////////////////
// FILE #2: FCClientOrder.cpp
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FCClientOrder.h"

namespace FC
{

    ClientOrder::ClientOrder()
    { ordStatus( '0' ); }

    ClientOrder::~ClientOrder()                                                     // Dtor
    {}

    ///////////////////////////////////////////////////////////////////////////
    // PUBLIC FUNCTIONS
    //

    void ClientOrder::change_status_static(std::tr1::shared_ptr<FC::ClientOrder> spOrder,char cOrdStatus)
    { spOrder->ordStatus( cOrdStatus ); }


    ///////////////////////////////////////////////////////////////////////////
    // GET/SET FUNCTIONS
    //

    std::string ClientOrder::clOrdID(void) const                                // Get Client Order ID
    { return m_sClOrdID; }
    void ClientOrder::clOrdID(const std::string& sClOrdID)
    { m_sClOrdID = sClOrdID; }

    char ClientOrder::ordStatus(void) const
    { return m_cOrdStatus; }
    void ClientOrder::ordStatus(char cOrdStatus)
    { m_cOrdStatus = cOrdStatus; }

} // end of namespace FC





///////////////////////////////////////////////////////////////////////////////
// FILE #3: MIC Definition, and member declaration
///////////////////////////////////////////////////////////////////////////////

    typedef std::tr1::shared_ptr<FC::ClientOrder>           spClientOrder;
    typedef boost::multi_index::multi_index_container
    <
        spClientOrder,                                                      // Contained type, shared pointer to ClientOrder
        boost::multi_index::indexed_by
        <
            boost::multi_index::ordered_unique                              // 0 index - shared pointer index
            <
                boost::multi_index::identity<spClientOrder>,
                std::less<spClientOrder>
            > ,
            boost::multi_index::ordered_unique                              // 1 index - ClientOrderID
            <
                boost::multi_index::const_mem_fun< FC::ClientOrder, std::string, &FC::ClientOrder::clOrdID >
            > ,
            boost::multi_index::ordered_non_unique                          // 2 index - Order Status
            <
                boost::multi_index::const_mem_fun< FC::ClientOrder, char, &FC::ClientOrder::ordStatus > ,
                std::less<char>
            > 
        >
    > OrderMIC;





    // Member in working class
    OrderMic        m_micOrders;






///////////////////////////////////////////////////////////////////////////////
// FILE #4: Utility Functions to update the MIC via .modify()
///////////////////////////////////////////////////////////////////////////////

    bool XMLTestApp::updateOrder(spClientOrder spOrder,char cOrdStatus)                 // Order Status is an index, so you must modify via MIC member functions
    {
        OrderMIC::nth_index<0>::type& index = m_micOrders.get<0>();
        OrderMIC::nth_index<0>::type::iterator i = index.find( spOrder );
        OrderMIC::nth_index<0>::type::iterator iEnd = index.end();
        if ( i != iEnd )
        {
            // FOUND
            // i points at the order to change
            return m_micOrders.modify(i,FC::ClientOrder::change_status_by_sp(cOrdStatus));
        }

        return false;
    }


    bool XMLTestApp::updateOrder(const std::string& sClOrdID,char cOrdStatus)                   // Order Status is an index, so you must modify via MIC member functions
    {
        OrderMIC::nth_index<1>::type& index = m_micOrders.get<1>();
        OrderMIC::nth_index<1>::type::iterator i = index.find( sClOrdID );
        OrderMIC::nth_index<1>::type::iterator iEnd = index.end();
        if ( i != iEnd )
        {
            // FOUND

            // Option1 - This works, but does another lookup via alternative fn
            return updateOrder( *i,cOrdStatus );                                                // <- Works

            // Option2 - Attempt to use static fn, fails
            //spClientOrder spOrder = *i;
            //return m_micOrders.modify(i,FC::ClientOrder::change_status_static(spOrder,cOrdStatus));

            // Option3 - Attempt to use *i as spOrder, fails
            //return m_micOrders.modify(i,FC::ClientOrder::change_status_by_sp(cOrdStatus));
        }

        return false;
    }




// FILE #5: Compile Error

1>------ Build started: Project: XMLTest, Configuration: Debug Win32 ------
1>Compiling...
1>XMLTestApp.cpp
1>c:\program files\microsoft visual studio\myprojects\xmltest\xmltest\xmltestapp.cpp(931) : error C2664: 'bool boost::multi_index::detail::ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>::modify<FC::ClientOrder::change_status_by_sp>(boost::multi_index::detail::bidir_node_iterator<Node>,Modifier)' : cannot convert parameter 1 from 'boost::multi_index::detail::bidir_node_iterator<Node>' to 'boost::multi_index::detail::bidir_node_iterator<Node>'
1>        with
1>        [
1>            KeyFromValue=boost::multi_index::identity<FC::XMLTestApp::spClientOrder>,
1>            Compare=std::less<FC::XMLTestApp::spClientOrder>,
1>            SuperMeta=boost::multi_index::detail::nth_layer<1,FC::XMLTestApp::spClientOrder,boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<FC::XMLTestApp::spClientOrder>,std::less<FC::XMLTestApp::spClientOrder>>,boost::multi_index::ordered_unique<boost::multi_index::const_mem_fun<FC::ClientOrder,std::string,::>>,boost::multi_index::ordered_non_unique<boost::multi_index::const_mem_fun<FC::ClientOrder,char,::;>,std::less<char>>>,std::allocator<FC::XMLTestApp::spClientOrder>>,
1>            TagList=boost::mpl::vector0<boost::mpl::na>,
1>            Category=boost::multi_index::detail::ordered_unique_tag,
1>            Node=boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<FC::XMLTestApp::spClientOrder,std::allocator<FC::XMLTestApp::spClientOrder>>>>>,
1>            Modifier=FC::ClientOrder::change_status_by_sp
1>        ]
1>        and
1>        [
1>            Node=boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<FC::XMLTestApp::spClientOrder,std::allocator<FC::XMLTestApp::spClientOrder>>>>
1>        ]
1>        and
1>        [
1>            Node=boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<FC::XMLTestApp::spClientOrder,std::allocator<FC::XMLTestApp::spClientOrder>>>>>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Program Files\Microsoft Visual Studio\MyProjects\XMLTest\XMLTest\Debug\BuildLog.htm"
1>XMLTest - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

//解决方案(由stefaanv提供)

我使用modify()作为m_micOrders的成员函数。我应该一直使用modify()作为迭代器应用的索引对象的成员函数,这样updateOrder()函数应该被修正为这样......

bool XMLTestApp::updateOrder(const std::string& sClOrdID,char cOrdStatus)
    { 
        OrderMIC::nth_index<1>::type& index = m_micOrders.get<1>(); 
        OrderMIC::nth_index<1>::type::iterator i = index.find( sClOrdID ); 
        OrderMIC::nth_index<1>::type::iterator iEnd = index.end(); 
        if ( i != iEnd ) 
        { 
            // FOUND 
            return index.modify(i,FC::ClientOrder::change_status_by_sp(cOrdStatus)); 
        } 
        return false; 
    } 

1 个答案:

答案 0 :(得分:0)

(由原始问题复制的stefaanv提供的解决方案。我听到这个问题,以便问题不再显示为未答复。@ {{3} }:随意再次将其复制为另一个答案并接受它。)

modify()被用作m_micOrders的成员函数。相反,modify()应该被用作迭代器应用的索引对象的成员函数,这样updateOrder()函数应该被修正为这样......

bool XMLTestApp::updateOrder(const std::string& sClOrdID,char cOrdStatus)
    { 
        OrderMIC::nth_index<1>::type& index = m_micOrders.get<1>(); 
        OrderMIC::nth_index<1>::type::iterator i = index.find( sClOrdID ); 
        OrderMIC::nth_index<1>::type::iterator iEnd = index.end(); 
        if ( i != iEnd ) 
        { 
            // FOUND 
            return index.modify(i,FC::ClientOrder::change_status_by_sp(cOrdStatus)); 
        } 
        return false; 
    }