0

以下のコードがありますが、「int age」に何か問題があるようです。コードは次のとおりです。

struct MyStruct
{
    char* firstName;
    char* secondName;
    int age;
};

typedef composite_key
    <MyStruct*,
    BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, firstName),
    BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, secondName),
    BOOST_MULTI_INDEX_MEMBER(MyStruct, int, age)
    > comp_key;


struct CompareLess
{   // functor for operator<=

    static inline int compare(const char* left, const char* right)
    {
        return strcmp(left, right);
    }
    inline bool operator()(const char* left, const char* right) const
    {   // apply operator<= to operands
        return compare(left, right)<0;
    }
    static inline int compare(const MyStruct* myStruct1, const MyStruct* myStruct2)
    {
        int result= compare(myStruct1->firstName, myStruct2->firstName);
        if(result!=0)
            return result;
        else
        {
            return compare(myStruct2->secondName, myStruct2->secondName);
        }
    }
    inline bool operator()(const MyStruct* myStruct1, const MyStruct* myStruct2)
    {
        return compare(myStruct1, myStruct2)<0;
    }
};

typedef multi_index_container
    <
    MyStruct*, 
    indexed_by
        <
        ordered_unique
            <
                comp_key,
                /*CompareLess*/
                composite_key_compare
                <
                    CompareLess,
                    CompareLess,
                    std::less<int>
                >
            >
        >
    > MyContainer;



boost::ptr_vector<MyStruct> vec;
MyStruct* struct1=new MyStruct();
struct1->firstName="Michael";
struct1->secondName="Mike";
struct1->age=20;
vec.push_back(struct1);



MyContainer myContainer;
myContainer.insert(struct1);
char* first="Michael";
char* second="Mike";
string michael="Michael";
auto it=myContainer.find(boost::make_tuple(michael.c_str(), (const char*)second,20);
if(it!=myContainer.end())
    cout << (*it)->age << endl;

問題は「(boost::make_tuple(michael.c_str(), (const char*)second),20)」で、「20」が取り込めないようです。詳細なエラーは次のとおりです。

C:\boost_1_52\boost/multi_index/composite_key.hpp(381): エラー C3849: 型 'int' の式に対する関数スタイルの呼び出しは、利用可能な 3 つの演算子オーバーロードすべての const および/または volatile 修飾子を失います 1> C:\ boost_1_52\boost/multi_index/composite_key.hpp(380) : クラス テンプレート メンバ関数のコンパイル中 'bool boost::multi_index::detail::compare_ckey_ckey_normal::compare(const KeyCons1 &,const Value1 &,const KeyCons2 &,const Value2 & ,const CompareCons &)'

とにかくこれを回避する方法はありますか?

4

1 に答える 1

1

些細な構文エラー (括弧の欠落) を修正し、#includes などを追加した後、ここで動作します (MSVC 2012.)。

于 2013-07-20T22:21:40.817 に答える