2013-07-20 29 views
0

我有下面的代码,但似乎什么是错用 '诠释时代',代码如下:关于Boost :: multi_index_container,错误c3849,绑定到int有什么问题?

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; 

问题是与“(升压:: make_tuple(michael.c_str(),(为const char *)第二),20)',看起来它不能接受'20'。详细错误如下:

C:\ boost_1_52 \ boost/multi_index/composite_key.hpp(381):错误C3849:对类型为'int'的表达式的函数式调用会丢失const和/或volatile限定符对于所有3个可用的操作符过载 1> C:\ boost_1_52 \ boost/multi_index/composite_key.hpp(380):编译类模板成员函数'bool boost :: multi_index :: detail :: compare_ckey_ckey_normal :: compare(const KeyCons1 & ,常量值1 &,常量KeyCons2 &,常量值2 &,常量CompareCons &)”

反正是有解决此问题?

+0

括号中放错了地方的boost :: make_tuple(michael.c_str(),( const char *)second,20) –

+0

谢谢,我justed编辑。但问题依然存在。 – Michael

+1

做它说的,将MyStruct中的'char *'改为'const char *'。 –

回答

1

固定一个平凡的语法错误(丢失的圆括号),并加入#include小号等之后,事情的作品在这里(MSVC 2012)