gpt4 book ai didi

c++ - 再次关于 Boost::multi_index_container,错误 c3849,绑定(bind)到 int 有什么问题?

转载 作者:行者123 更新时间:2023-11-28 07:30:51 26 4
gpt4 key购买 nike

我有下面的代码,但是'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 &)'

有没有办法解决这个问题?

最佳答案

在修复了一个小语法错误(缺少括号)并添加了 #include 等之后,这里就可以正常工作了 (MSVC 2012.)

关于c++ - 再次关于 Boost::multi_index_container,错误 c3849,绑定(bind)到 int 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17762422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com