自分のバグ/問題がどこにあるのかわからないので困惑しています。2つのカスタム演算子、1つの割り当てと1つの比較演算子を使用するクラスInstructionがあります。以前は、std :: sortを使用して、メンバーの1つであるstd :: string nameに基づいて命令をソートするために、比較演算子のみを使用していました。ただし、プロジェクト全体のリファクタリングを開始したため、一部のメンバーを定数に変更しました。これにより、これらの定数に初期化リストを使用する必要がありました。これにより、代入演算子を作成する必要があります。これらの命令は、ベクトルにプッシュバックされるときにコピーされるためです。これはすべてがうまくいかないところです。クラス宣言とコンストラクターと演算子を含めます。
Instruction.hpp
class Instruction
{
private:
unsigned int param_size;
const float max_angle, min_angle;
bool micro_mutated;
protected:
const std::string body_part;
std::vector<Parameter <float> > parameters;
public:
Instruction(std::string name, float max, float min);
Instruction operator=(const Instruction& I);
bool operator<(const Instruction& I) const;
//there are a few more functions but are completely irrelevant
}
Instruction.cpp:
Instruction::Instruction(std::string name,float max, float min) :
body_part (name), max_angle(max), min_angle(min)
{}
Instruction Instruction::operator=(const Instruction& I)
{
(*this) = I;
return (*this);
}
bool Instruction::operator<(const Instruction& I) const
{
return body_part < I.body_part;
}
代入演算子を作成した唯一の理由(正直なところ、これまでに作成したことはありません)は、命令をプッシュバックしようとしたときに、コンパイラが「ここから」命令をインスタンス化できないと文句を言ったためです。定数メンバーで行います。メンバーが一定でなくても、並べ替えもすべて正常に機能しました。今、奇妙な部分。std :: sortを削除すると、上記のコードは機能しますが、常に機能するわけではありません。しばらくするとクラッシュすることもあれば、クラッシュしないこともあります。しかし、並べ替えを含めると、すぐにクラッシュします。誰か助けてもらえますか?