2012-11-30 5 views
2

내가 원하는 것은 구조가 조건부로 만족되면 구조체의 한 필드를 대체하는 것입니다. 그래서 여기 내 코드입니다 : 해당 항목의 제기 펩타이드는 "테스트"와 같은 경우는 내가 11 스펙트럼 항목의 모든 Duplicate_Nu을 변경하려면이 examle에 따라서구조체에 대한 std :: replace_if

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); 
. 
. 
} 

하지만 내가 원하는 동안 다음 오류가 발생했습니다 "="

/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&’

답변

4

지금까지 문제는 당신이 사본이 아닌 복사 가능한 개체를 전달하려고하는 것입니다 대신 작동 기능 GetByT를 사용합니다. 이는 boost::bind()에 대한 인수로 제공 한 모든 내용이 복사되기 때문입니다.

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

은 당신이 당신이 사본 통과하지 않으려면해야 할 일은 (어떤 해를하지 않을 것이다 포인터를 복사) 포인터로 전달하는 것입니다. 다른 방법으로는 예를 들어, 참조로 전달하는 boost::ref을 사용할 수 있습니다 :

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

또 다른 문제는 당신이 std::replace_if() 마지막 인수로 11를 지정합니다. 요소가 대체되어야하는 값입니다 (술어가 true을 리턴하는 경우). 배열에 저장된 객체와 동일한 유형이어야합니다 (또는 변환 가능). 그러나 11 (보통 부호가있는 정수인 int)을 DataST 유형의 개체로 변환 할 수 없습니다.

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); 
+0

없음 문제가 계속 (동일한 오류로) 남아있다 – khikho

+0

@khikho :이 같은 뭔가가 필요 엡, 엡, 미안 - 내가 답을 드리고 있습니다. –

+0

** 업데이트 부분 ** 나는 ** 데이터 구조를 갱신 할 수 없습니다. 왜냐하면 if 선언문이 true를 반환하는 구조체를 업데이트하기 위해서입니다. (이 경우 단지 ** Duplicate_Nu **를 업데이트하고 싶습니다) . [3은 GetByT 함수의 인수이며 대체 값이 아닙니다. – khikho

관련 문제