【问题标题】:std vector no match for operator ==标准向量与运算符不匹配 ==
【发布时间】:2015-07-15 09:36:31
【问题描述】:

我被这个错误困住了:

gcc.compile.c++ Physics/HelicityAmplitude/bin/gcc-4.8.3/debug/HelicityDecayTree.o 在 包含的文件 /cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/algorithm:62:0, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/move/algorithm.hpp:23, 从 /cluster/compwa_externals/boost_1_55_0/include/boost/move/move.hpp:24, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/util.hpp:19, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/buckets.hpp:14, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/table.hpp:10, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/unordered/detail/equivalent.hpp:14, 来自 /cluster/compwa_externals/boost_1_55_0/include/boost/unordered/unordered_set.hpp:17, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/unordered_set.hpp:16, 来自/cluster/compwa_externals/boost_1_55_0/include/boost/graph/adjacency_list.hpp:21, 来自物理/HelicityAmplitude/HelicityDecayTree.hpp:17, 来自 Physics/HelicityAmplitude/HelicityDecayTree.cpp:12:

/cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/bits/stl_algo.h:在 '_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: ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterato r >; _Tp = HelicityFormalism::ParticleState]’ Physics/HelicityAmplitude/HelicityDecayTree.cpp:59:61:需要从 这里

/cvmfs/cluster/gcc/gcc-4.8.3/include/c++/4.8.3/bits/stl_algo.h:166:17: 错误:'operator==' 不匹配(操作数类型为 ‘HelicityFormalism::ParticleState’ 和 ‘const Helicit yFormalism::ParticleState’) if (*__first == __val)

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

类的标题:

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 方法要求为 1 个参数提供非 const 版本...

提前谢谢, 史蒂夫

【问题讨论】:

  • 您没有显示在 HelicityFormalism::ParticleState 上运行的 operator==() 的声明,而是显示了很多似乎与实际错误无关的东西。
  • ParticleState 是否支持 == 方法?
  • 我添加了这个定义。我认为编译器会为该类/结构生成一个默认运算符 ==,并对所有成员进行值比较。
  • @steve:不,这不会发生。如果您需要类类型的比较运算符,则必须提供它们。
  • 啊该死的,又搞混了。好的谢谢大家。

标签: c++ vector constants operator-keyword


【解决方案1】:

好吧,我忘了编译器不会合成 operator==。所以我只是失踪了

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

【讨论】:

  • 您应该添加一个const 限定符,否则您将无法比较常量对象。
  • 真的...,改了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 2013-09-29
相关资源
最近更新 更多