English 中文(简体)
如何调用一个推进多指数元素的非默认成员功能
原标题:how to call non-constant member function of a boost multi_index element

我张贴这个是因为我无法理解 助推辅导如何运作。

我有一个类别,其对象 是一个推进多指数容器的元素。

I need to update the member variables of objects using member functions. I don t know how to do that. could you help me please. I prepared a simple example:

#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include<vector>

using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::member;

class employee_entry
{
public:
   employee_entry( const std::string& first,
                   const std::string& last,
                   long id):
                   first_name_(first),
                   last_name_(last),
                   id_(id)
   {}
   void change(){id_++;}//causing the problem
   std::string first_name_;
   std::string last_name_;
   std::vector<int> mySet;
   long id_;

   std::vector<int>::iterator mySet_begin()  {return mySet.begin();   }
};


typedef multi_index_container<
employee_entry, indexed_by<
   ordered_unique<member<employee_entry, std::string, &employee_entry::first_name_> >
   , ordered_non_unique<member<employee_entry, std::string, &employee_entry::last_name_> >
   , ordered_non_unique<member<employee_entry, long, &employee_entry::id_> >
   >
> employee_set;

//employee set.... multi-index
employee_set m_employees;


int main()
{
   using boost::multi_index::nth_index;
   using boost::multi_index::get;

   typedef nth_index<employee_set, 0>::type first_name_view;
   first_name_view& fnv = get<0>(m_employees);

   fnv.insert(employee_entry("John", "Smith", 110));
   fnv.insert(employee_entry("Fudge", "Hunk", 97));

   ///get employees sorted by id
   typedef nth_index<employee_set, 2>::type id_view;
   id_view& idv = get <2> (m_employees);
   for(id_view::reverse_iterator it = idv.rbegin(), it_end(idv.rend()); it != it_end; ++it)
   {
       std::cout << it->first_name_  <<" "
                 << it->last_name_ << ":"
                 << it->id_ << std::endl;
       it->change();//calling the troublesome function
   }

   return 0;
}

产生的错误是:

 $c++ dr_function.cpp 
dr_function.cpp: In function ‘int main()’:
dr_function.cpp:65:19: error: passing ‘const employee_entry’ as ‘this’ argument of ‘void employee_entry::change()’ discards qualifiers [-fpermissive]
问题回答

您张贴的解决方案不会起作用 : 最多它会得到一个模糊的索引, 最坏情况下你的应用程序会崩溃 。 您的最后一个索引取决于 employee_ entry:: id_ / code>, 您无法自由更改它, 因为您再次默认地打破了索引的顺序 。 对于这类事项 Boust. MultiIndex 提供更新功能 replace modify , 正如所讨论的 < a href=> http://www.boost.org/libs/multi_index/doc/tutorive/ basics.html# ord_updting" rel=“ no folfol” >here 。 在您的特殊情况下, 您可以将您的 < code> changer 成员函数调:

idv.modify(idv.iterator_to(*it),boost::bind(&employee_entry::change,_1));

略加解释 : idv.eiterator_to(*it) 只是将您的反向代号转换成一个常规代号, 这就是 modify 的需要。 至于 boost:: bind 部分, 此封装 变成一个合适的修正调控真菌。 这样, 您可以让 Boost. MultiIndex 了解即将在 id_ {/code> 中发生的变化, 并相应更新索引 。

well, I am answering my question at least to debug the above mentioned code. My main concern was discussed and answered in the comments below the question. I the following code, I made long id_ as mutable and changed void change(){...} to void change()const{...}

#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include<vector>

using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::member;

class employee_entry
{
public:
   employee_entry( const std::string& first,
                   const std::string& last,
                   long id):
                   first_name_(first),
                   last_name_(last),
                   id_(id)
   {}
   void change() const {id_++;}//causing the problem
   std::string first_name_;
   std::string last_name_;
   std::vector<int> mySet;
   mutable long id_;

   std::vector<int>::iterator mySet_begin()  {return mySet.begin();   }
};


typedef multi_index_container<
employee_entry, indexed_by<
   ordered_unique<member<employee_entry, std::string, &employee_entry::first_name_> >
   , ordered_non_unique<member<employee_entry, std::string, &employee_entry::last_name_> >
   , ordered_non_unique<member<employee_entry, long, &employee_entry::id_> >
   >
> employee_set;

//employee set.... multi-index
employee_set m_employees;


int main()
{
   using boost::multi_index::nth_index;
   using boost::multi_index::get;

   typedef nth_index<employee_set, 0>::type first_name_view;
   first_name_view& fnv = get<0>(m_employees);

   fnv.insert(employee_entry("John", "Smith", 110));
   fnv.insert(employee_entry("Fudge", "Hunk", 97));

   ///get employees sorted by id
   typedef nth_index<employee_set, 2>::type id_view;
   id_view& idv = get <2> (m_employees);
   for(id_view::reverse_iterator it = idv.rbegin(), it_end(idv.rend()); it != it_end; ++it)
   {
       std::cout << it->first_name_  <<" "
                 << it->last_name_ << ":"
                 << it->id_ << std::endl;
       it->change();//calling the troublesome function
   }

   return 0;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?