...tror jag iaf.
Detta är ett test av en klass vars objekt är tänkta att fungera som extremt långa integers.
typedef int boolean;
#define false 0
#define true 1
class OsaouInt{
public:
OsaouInt(char startsumma[]="0"){
if (startsumma=="0") num_length = 0;
else{
int i = strlen(startsumma);
num_length = i;
numbers = new int[i];
for (; --i>=0;) numbers[i] = ((int)startsumma[i])-48;
}
}
long length(void){
return num_length;
}
void print(boolean with_commas=false){
for (int i=-1; ++i<num_length;){
cout << numbers[i];
if (with_commas && ((num_length-i)%3==0) && (i<num_length-2))
cout << ".";
}
}
OsaouInt& operator =(const OsaouInt& other){
if (this==&other) return *this;
int i;
for (i=-1; ++i<num_length;) numbers[i] = NULL;
num_length = other.num_length;
for (i=-1; ++i<num_length;) numbers[i] = other.numbers[i];
return *this;
}
friend OsaouInt operator +(OsaouInt& other1, OsaouInt& other2);
private:
int * numbers;
long num_length;
};
OsaouInt operator +(OsaouInt& other1, OsaouInt& other2){
OsaouInt res;
long temp_l = other2.length();
int temp_v, temp_m = 0;
if (other1.length()>other2.length()) temp_l = other1.length();
int i = -1;
while (++i<temp_l || temp_m>0){
if (other1.numbers[i]==NULL) res.numbers[i] = other2.numbers[i];
else if (other2.numbers[i]==NULL) res.numbers[i] = other1.numbers[i];
else{
temp_v = 0;
if (temp_m>0) temp_v = temp_m;
temp_m = 0;
temp_v += other1.numbers[i]+other2.numbers[i];
if (temp_v>9){
temp_v -= 9;
temp_m = temp_v;
}
res.numbers[i] = temp_v;
}
}
res.num_length = i-1;
return res;
}
void main(void){
OsaouInt myBigInt("53008"), int1;
int1 = myBigInt + "90"; // Här får jag nedanstående error...
myBigInt.print();
int1.print();
getche();
}
Och detta är felmeddelandet jag får:
"binary '+' : no operator defined which takes a right-hand operand of type 'char [3]' (or there is no acceptable conversion)"
Ja, jag vet att print inte riktigt funkar än, men det hör inte till diskussionen.. ;)