falscher return value durch Methodenaufruf in Operatorüberladung
-
Hallo, ich habe ein (für mich) sehr interessantes Problem.
In einer Klasse zur "verwaltung" eines boolschen arrays, möchte ich die führenden Nullen killen. So habe ich folgende Methode:void whole_number_type::clr_leading_zeros(void) { //first we have to check the most significant setted bit cout<<endl<<"input: "; this->print(); long int msb=0; for(long int i=0; i<this->count;i++) {if(this->pBinary_array[i]){msb=i;}}; if(msb!=(this->count-1)) { bool *newArray; newArray=new bool[msb+1]; for(long int i=0; i<msb+1;i++) {newArray[i]=this->pBinary_array[i];}; delete[] pBinary_array; this->count=msb+1; for(long int i=0; i<count;i++) { this->pBinary_array[i]=newArray[i];}; } cout<<endl<<"output:"; this->print(); }rufe ich dies innerhalb einer anderen methode, z.b.: bei der operator überladung + auf (nicht aufregen, in der op-ü habe ich gleich die methode der summation mit reingebracht, daher...):
whole_number_type whole_number_type::operator+(const whole_number_type &obj) const{ . . . cout<<endl<<"temp b4 clr"; temp.print(); cout<<endl<<"clr leading zeros"; temp.clr_leading_zeros(); cout<<endl<<"temp array:"; temp.print(); return temp; };kommt folgende ausgabe:
Konsole schrieb:
temp b4 clr
LSB: 0011000 :MSB
clr leading zeros
input:
LSB: 0011000 :MSB
output:
LSB: 0011 :MSB
temp array:
LSB: 0011 :MSB
= routine
LSB: 0000 :MSB
o=n+n:
LSB: 0000 :MSB
clr leading 0's of o:
input:
LSB: 0000 :MSB
output:
LSB: 0 :MSB
o after clr:
LSB: 0 :MSBo sollte dabei der temp value entsprechen.
o in der main:
int main(int argc, char *argv[]){ . . . o=n+n; cout<<endl<<"o=n+n:"; o.print(); cout<<endl<<"clr leading 0's of o:"; o.clr_leading_zeros(); cout<<endl<<"o after clr:"; o.print(); cout<<endl; system("PAUSE"); return EXIT_SUCCESS; }lasse ich das clr_leading_zeros in op+ weg bekomme ich folgende ausgabe:
Konsole schrieb:
temp b4 clr
LSB: 0011000 :MSB
clr leading zeros
temp array:
LSB: 0011000 :MSB
= routine
LSB: 0011000 :MSB
o=n+n:
LSB: 0011000 :MSB
clr leading 0's of o:
input:
LSB: 0011000 :MSB
output:
LSB: 0011 :MSB
o after clr:
LSB: 0011 :MSBund ich weiß nicht woran das liegt. Falls ich mich bzgl. des Problems falsch ausgedrückt haben sollte bitte nochmal nachfragen,..
gruß und dank
-
//hier wirds geloescht... delete[] pBinary_array; this->count=msb+1; for(long int i=0; i<count;i++) { //...und hier trotzdem benutzt this->pBinary_array[i]=newArray[i]; };Einfacher und richtig wäre:
pBinary_array = newArray;PS: Rücke das nächste Mal bitte vernünftig ein. Den Kot zu lesen ist nicht so dolle.
-
Jo,
aua stimmt, habepBinary_array =new bool[msb+1];vergessen.
war schon spät gestern.
danke nochmal