我为 >> 和 << 实现运算符重载
<< 好像没有问题
但是为了实现 operator>> 如下所示,我遇到了运行时错误
QDataStream & operator>> (QDataStream & stream, chromosome & myChromosome){
myChromosome = chromosome();
double len, wing1, wing2, tail, head;
int index;
stream >> head >> len >> tail >> wing1 >> wing2 >> index;
myChromosome.setChromosomeHeadLength(head);
myChromosome.setChromosomeLength(len);
myChromosome.setChromosomeTailLength(tail);
myChromosome.setChromosomeWing1Length(wing1);
myChromosome.setChromosomeWing2Length(wing2);
myChromosome.setIndex(index);
return stream;
}
我调试这部分,程序停止(运行时错误),当我到达myChromosome.setChromosomeHeadLength(head);
编辑: 染色体代码:
chromosome::chromosome()
{
chromosomeLength = 0;
chromosomeHeadLength = 0;
chromosomeTailLength = 0;
chromosomeWing1Length = 0;
chromosomeWing2Length = 0;
index = 0;
topLeftX=0;
topLeftY=0;
bottomRightX=0;
bottomRightY=0;
}
...
void chromosome::setChromosomeLength(double lenght){
chromosomeLength = lenght;
}
void chromosome::setChromosomeHeadLength(double lenght){
chromosomeHeadLength = lenght;
}
void chromosome::setChromosomeTailLength(double lenght){
chromosomeTailLength = lenght;
}
void chromosome::setChromosomeWing1Length(double lenght){
chromosomeWing1Length = lenght;
}
void chromosome::setChromosomeWing2Length(double lenght){
chromosomeWing2Length = lenght;
}
当我运行程序时,错误是: myProgram.exe 已停止工作
当我调试它时,我得到了这个
编辑 2:更多代码,使用 operator>> 和 <<
void TabView::save(QDataStream &stream)
{
stream << numberOfChromosomes;
for(int i = 0; i < numberOfChromosomes; i++){
stream << chromosomes[i];
}
...
}
bool TabView::load(QDataStream &stream){
stream >> numberOfChromosomes;
for(int i = 0; i < numberOfChromosomes; i++){
stream >> chromosomes[i];
}
...
}
任何想法表示赞赏。