2
votes

The thing what I want to do is just replacing one field of structure on the vector of structure if the condition was satisfied . So here is my code:

struct DataST{
    int S_num,Charge,Duplicate_Nu;
    float PEP;
    string PEPTIDE;
    vector<MZIntensityPair> pairs;
    bool GetByT(const DataST& r,int T)
    {
      switch (T)
      {
          case 1:
            return (S_num == r.S_num);
          case 2:
            return (Charge == r.Charge);
          case 3:
            return !(PEPTIDE.compare(r.PEPTIDE));
          case 4:
            return (Duplicate_Nu == r.Duplicate_Nu);
          case 5:
            return ((S_num == r.S_num)&&(Charge == r.Charge));
          default:
            return false;
      }
    }
  };
int main()
{
 .
 .
 vector<DataST> spectrums;
 .
 .
 DataST tempDT_dup;
 tempDT_dup.PEPTIDE="Test";
 replace_if(spectrums.begin(), spectrums.end(), boost::bind(&DataST::GetByT, _1,tempDT_dup,3),11);
 .
 .
}

So in this examle I want to change all the Duplicate_Nu of spectrums items with 11 if the PEPTIDE filed of that item is equal to "test" but I got error below while I want to use function GetByT instead of operation "="

/usr/include/c++/4.6/bits/stl_algo.h:4985:4: error: no match for ‘operator=’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = DataST*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = DataST& = __new_value’ /usr/include/c++/4.6/bits/stl_algo.h:4985:4: note: candidate is: hello_pwiz/hello_pwiz.cpp:14:8: note: DataST& DataST::operator=(const DataST&) hello_pwiz/hello_pwiz.cpp:14:8: note: no known conversion for argument 1 from ‘int DataST::* const’ to ‘const DataST&’

1

1 Answers

4
votes

The problem thus far is that you are trying to pass a non-copyable object by copy. This is because anything that you supply as arguments to boost::bind() is being copied.

boost::bind(&DataST::GetByT, _1,tempDT_dup,3),
                                 /|\
                                  |
  This means pass by copy. --------

What you have to do if you do not wish to pass by copy is to pass by pointer (copying a pointer won't do any harm). Alternatively, you may use boost::ref to pass by reference, for example:

boost::bind(&DataST::GetByT, _1,boost::ref(tempDT_dup),3),

Another problem is that you specify 11 as the last argument to std::replace_if(). It is a value that an element should be replaced with (if predicate returns true). It should be of the same type as objects stored in your array (or be convertible to). But you cannot convert 11 (which is a plain signed integer aka int) to an object of type DataST. You need something like this:

vector<DataST> spectrums;
DataST tempDT_dup;
DataST replacee; // What you want to replace with...
replace_if(spectrums.begin(), spectrums.end(),
           bind(&DataST::GetByT, _1, boost::ref(tempDT_dup), 3),
                replacee);