Currently getting an 'Expected unqualified-id' error before '{' when trying to overload the + operator in the implementation file. I'm guessing this is also causing the 'Function 'operator+' is not implemented' error I'm getting in the header as well. I'm not quite sure what's gone wrong here and how I would fix it.
Header file:
#ifndef BIGINT_BIGINT_H
#define BIGINT_BIGINT_H
#include <iostream>
class Bigint {
public:
// the input operator
friend std::istream& operator>> (std::istream& in, Bigint& n);
// the output operator
friend std::ostream& operator<< (std::ostream& out, const Bigint& n);
// the + operator
friend Bigint operator+ (const Bigint& n1, const Bigint& n2);
private:
int digits [256]; // array number will be stored in
int size; // size of number
};
#endif //BIGINT_BIGINT_H
Implementation file:
#include "Bigint.h"
#include <iostream>
std::istream& operator>> (std::istream& in, Bigint& n) {
char number[256];
in.get(number, 256, ' ');
int size = 0;
while(number[size] != '\0'){
size++;
}
for(int i = 0; i < size; i++){
n.digits[i] = number[size - i - 1] - '0';
}
n.size = size;
return in;
}
std::ostream& operator<< (std::ostream& out, const Bigint& n) {
for(int i = 0; i < n.size; i++){
out << (n.digits[n.size - i -1]);
}
return out;
}
Bigint operator+ (const Bigint& n1, const Bigint& n2); {
for (int i = 0; i < n2.size; i++){
int sum = n1.digits[i] + n2.digits[i];
if (sum > 9) {
sum = sum % 10;
n1.digits[i] = sum;
n1.digits[i + 1] += 1;
}
else {
n1.digits[i] = sum;
}
}
return n1;
}
Thanks.
Aucun commentaire:
Enregistrer un commentaire