When I insert in the main function one after the other elements are inserted properly but when I try to do that through a function all values get replaced by the last elements.
Please refer the following code :
struct X
{
std::string panelid; // assume unique
std::string messageid; // assume unique
std::string tid; // assume non-unique
std::string name;
};
struct IndexByPanelID {};
struct IndexByMessageID {};
struct IndexByTID {};
typedef boost::multi_index_container<
X*, // the data type stored
boost::multi_index::indexed_by<
boost::multi_index::hashed_non_unique<
boost::multi_index::tag<IndexByPanelID>,
boost::multi_index::member<X, std::string, &X::panelid>
>,
boost::multi_index::hashed_non_unique<
boost::multi_index::tag<IndexByMessageID>,
boost::multi_index::member<X, std::string, &X::messageid>
>,
boost::multi_index::hashed_non_unique<
boost::multi_index::tag<IndexByTID>,
boost::multi_index::member<X, std::string, &X::tid>
>
>
> Container;
int Insert(X newframe, Container *Cont)
{
auto& indexByL = c.get<IndexByPanelID>();
indexByL.insert(&newframe);
return 0;
}
int main()
{
Container c; // empty container
X x1{ "1", "80", "FE01", "0712"};
X x2{ "2", "80", "FE02", "0713"};
X x3{ "3", "180", "FE03", "0714"};
X x4{ "4", "80", "FE04", "0715"};
Insert(x1,&c); // Doesnt work.
Insert(x2,&c); // Doesnt work.
Insert(x3,&c); // Doesnt work.
Insert(x4,&c); // Doesnt work.
auto& indexByI3 = c.get<IndexByPanelID>();
for(auto i = indexByI3.begin(); i != indexByI3.end(); i++)
{
X *x = *i;
std::cout << x->name << '\n';
std::cout << x->messageid << '\n';
std::cout << x->panelid << '\n';
std::cout << x->tid << '\n';
}
int main()
{
Container c; // empty container
X x1{ "1", "80", "FE01", "0712"};
X x2{ "2", "80", "FE02", "0713"};
X x3{ "3", "180", "FE03", "0714"};
X x4{ "4", "80", "FE04", "0715"};
// Insert some elements
auto& indexByL = c.get<IndexByPanelID>();
indexByL.insert(&x1); //works fine.
indexByL.insert(&x2); //works fine.
indexByL.insert(&x3); //works fine.
indexByL.insert(&x4); //works fine.
auto& indexByI3 = c.get<IndexByPanelID>();
for(auto i = indexByI3.begin(); i != indexByI3.end(); i++)
{
X *x = *i;
std::cout << x->name << '\n';
std::cout << x->messageid << '\n';
std::cout << x->panelid << '\n';
std::cout << x->tid << '\n';
}
}
Output 1st main :
0715
80
4
FE04
0715
80
4
FE04
0715
80
4
FE04
0715
80
4
FE04
2nd main :
0712
80
1
FE01
0713
80
2
FE02
0714
180
3
FE03
0715
80
4
FE04