std 向量与运算符不匹配 ==

std vector no match for operator ==

我被这个错误困住了:

gcc.compile.c++ Physics/HelicityAmplitude/bin/gcc-4.8.3/debug/HelicityDecayTree.o In file included from /cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/algorithm:62:0, from /cluster/compwa_externals/boost_1_55_0/include/boost/move/algorithm.hpp:23, from /cluster/compwa_externals/boost_1_55_0/include/boost/move/move.hpp:24, from /cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/util.hpp:19, from /cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/buckets.hpp:14, from /cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/table.hpp:10, from /cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/equivalent.hpp:14, from /cluster/compwa_externals/boost_1_55_0/include/boost/unordered/unordered_set.hpp:17, from /cluster/compwa_externals/boost_1_55_0/include/boost/unordered_set.hpp:16, from /cluster/compwa_externals/boost_1_55_0/include/boost/graph/adjacency_list.hpp:21, from Physics/HelicityAmplitude/HelicityDecayTree.hpp:17, from Physics/HelicityAmplitude/HelicityDecayTree.cpp:12:

/cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/bits/stl_algo.h: In instantiation of ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Tp = HelicityFormalism::ParticleState]’:

/cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/bits/stl_algo.h:4441:45: required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterato r >; _Tp = HelicityFormalism::ParticleState]’ Physics/HelicityAmplitude/HelicityDecayTree.cpp:59:61: required from here

/cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/bits/stl_algo.h:166:17: error: no match for ‘operator==’ (operand types are ‘HelicityFormalism::ParticleState’ and ‘const Helicit yFormalism::ParticleState’) if (*__first == __val)

我看到他要求对 ParticleState 进行 const 与 non-const 的比较。但是我真的不明白他为什么要进行这种比较。我的相关代码如下:

header为class:

class HelicityDecayTree {
  boost::adjacency_list<> decay_tree_;
  std::vector<ParticleState> particles_;

public:
  void createDecay(const ParticleState &mother,
  const ParticleStatePair &daughters);
}

以及该成员函数的来源:

void HelicityDecayTree::createDecay(const ParticleState &mother,
    const ParticleStatePair &daughters) {
  // add particles to the list
  unsigned int mother_vector_index;
  unsigned int daughter1_vector_index;
  unsigned int daughter2_vector_index;

  if (std::find(particles_.begin(), particles_.end(), mother)
      == particles_.end()) {
    mother_vector_index = particles_.size();
    particles_.push_back(mother);
  }
  else {
    mother_vector_index = std::distance(particles_.begin(),
        std::find(particles_.begin(), particles_.end(), mother));
  }
  if (std::find(particles_.begin(), particles_.end(), daughters.first)
      == particles_.end()) {
    daughter1_vector_index = particles_.size();
    particles_.push_back(daughters.first);
  }
  else {
    daughter1_vector_index = std::distance(particles_.begin(),
        std::find(particles_.begin(), particles_.end(), daughters.first));
  }
  if (std::find(particles_.begin(), particles_.end(), daughters.second)
      == particles_.end()) {
    daughter2_vector_index = particles_.size();
    particles_.push_back(daughters.second);
  }
  else {
    daughter2_vector_index = std::distance(particles_.begin(),
        std::find(particles_.begin(), particles_.end(), daughters.second));
  }

  // then make the correct inserts into the vector and link appropriately
  boost::add_edge(mother_vector_index, daughter1_vector_index, decay_tree_);
  boost::add_edge(mother_vector_index, daughter2_vector_index, decay_tree_);
}

以及 ParticleState 结构:

struct ParticleState {
  int particle_id_;
  std::string name_;
  Spin J_;
  Spin M_;
};

Afaiu 他应该为两个 const ParticleStates 合成 operator==,但出于某种原因,find 方法要求 non-const 版本的 1 个参数...

提前致谢, 史蒂夫

好吧我忘了编译器不会合成运算符==。所以我只是想念

bool operator==(const ParticleState &rhs) const {
  ...
}