dutch thanks it works but now this is coming up and it only comes up when i include the + operator in my main
cout<<"900+8000 = "<<(one+two)<<endl;
dbiguint operator +(const dbiguint &lhs, const dbiguint &rhs)
{
dbiguint one(lhs);
one += rhs;
return one;
}
a.out(39742,0x10d0c9dc0) malloc: *** error for object 0x7ff022c01700: pointer being freed was not allocated
a.out(39742,0x10d0c9dc0) malloc: *** set a breakpoint in malloc_error_break to debug
what does this error mean?
Im not sure what this means is their something wrong with my main? @dutch
@dutch
You're missing a copy constructor. Without it, when you do dbiguint one(lhs); you get the default copy ctor which just copies the data_ pointer, without creating a separate array. In that case, the data_ array gets deleted twice.
1 2 3 4 5 6 7 8
// copy constructor
dbiguint::dbiguint(const dbiguint& b)
{
capacity_ = b.capacity_;
data_ = newunsignedshort[capacity_];
for (size_t i = 0; i < capacity_; ++i)
data_[i] = b[i];
}
Also:
Your operator+= (and operator-=) should have return values of dbignuint& and need to return *this at the end.
Then operator+ can be written like this (note that lhs is passed by value, i.e., as a copy):
operators have fixed designs, eg you can't make operator + take 5 arguments.
+= needs to return something, because its an assignment.
x+=3; //this makes sense, its effectively x = x+3;
+=5; //what should this do? its like saying =5; you need something on both sides of assignments.
you can abuse operators a lot in c++ (its one of the things weak languages that prevent overloads claim is a negative) but there are some things you can do with normal functions that you can't force an operator to do.
you can probably force the code to do nothing with the returned value, though.
operator += (type thing) {cout << thing; return this;}
class.operator+=(athing); //dunno if you can make something like this compile and run or not. But really, with that kind of syntax (its close if not right? ) you may as well just make a non operator method to do whatever it was.
Obviously you can't return something if it's void.
But the += operator is not usually void.
Are you forced (by a moronic teacher) to declare it that way?
If not, then do it properly and return *this (as a reference, i.e., dbignuint&).