Errors with arrays and Overloading the << operator

ok the basic idea here was to loop through the array representing a big integer the problem is it doesn't compile at all i get teh following errors.

error: expected constructor, destructor, or type conversion before '&' token
error: expected ',' or ';' before '&' token



ostream& BigInt::operator<<(ostream & outs, BigInt& a)
{
for(int i = 0; i < a.size,i++)
{
outs<<a.data[i];
}
return outs;
}



i also am having a problem with the following line, i was trying to find the length of an array with the following code.

int i;
i = m.length; // m is an array of integers

but get the following error.

error: request for member 'length' in 'm', which is of non-class type 'int*'


any help would be greatly appreciated. I'll post the entire code incase the errors are coming from elsewhere.





////////////////////////////////////////////////////////////////////////////

#include "BigInt.h"
#include <cstdlib>
#include <cstdio>
using namespace std;


bool BigInt::operator<(const BigInt& b)
{
if(size > b.size)return false;
else if(size < b.size)return true;
else
{
for(int i = 0; i < size; i++){
if(data[i] < b.data[i]) return true;
if(data[i] > b.data[i]) return false;
}
return false;
}
}

bool BigInt::operator>(const BigInt& b)
{
if(size < b.size)return false;
else if(size > b.size)return true;
else
{
for(int i = 0; i < size; i++){
if(data[i] > b.data[i]) return true;
if(data[i] < b.data[i]) return false;
}
return false;
}
}

ostream& BigInt::operator<<(ostream & outs, BigInt& a)
{
for(int i = 0; i < a.size,i++)
{
outs<<a.data[i];
}
return outs;
}
BigInt::BigInt(int s,int d[])
{
size = s;
data = d;
}

//BigInt BigInt::operator*(const &BigInt b)
//{
//}

BigInt BigInt::operator+(const BigInt& b)
{
int* n;
int* m;

if(size > b.size){
n = new int[size];
m = data;
//create a new array full of zeros
for(int i = 0; i < size; i++)
n[i]=0;

for(int i = 1; i < b.size; i++)
{
n[size-i] = b.data[b.size-i];
}
}

if(size < b.size){
n = new int[b.size];
m = b.data;
//create a new array full of zeros
for(int i = 0; i < b.size; i++)
n[i]=0;

for(int i = 1; i < size; i++)
{
n[b.size-i] = b.data[size-i];
}
}

//add n and m
int* newdata;
newdata =new int[m.length + 1];
for(int i = 0; i < newdata.length; i++)
newdata[i]=0;
int carry = 0;
for(int i = m.size -1; i >=0; i--)
{
int x = 0;
x = n[i] + m[i]+ newdata[i];
newdata[i] = x%10;
if(x >= 10)newdata[i-1]= 1;
}
BigInt newBig = new BigInt(newdata.length,newdata);
return newBig;
}


BigInt BigInt::operator-(const BigInt& b)
{
int *m,*n,*newdata;

if(*this > b) // will result in positive number.
{
m[7];
m = new int[size];
n = new int[size];
newdata = new int[size];
if(size = b.size)
{
for(int i = size; i > 0 ; i--)
{
m[i-1]= data[i-1];
n[i-1]= b.data[i-1];
}
}
else
{
for(int i = size; i > 0 ; i--)
{
m[i-1]= data[i-1];
}
for(int i= 0; i < b.size ; i++)
{
n[size-i]= b.data[b.size-i];
}
} // at this point the numbers should be in 2 arrays of the same size.
for(int i = size-1; i>=0;i--) // this loop does the actual subtraction.
{
if(m[i] >= n[i])
{
newdata[i] = m[i] - n[i];
}
else
{
m[i-1] = m[i-1] - 1;
newdata[i] = (m[i] + 10) - n[i];
}
}
}
else // will result in negative number or 0.
{
m = new int[b.size];
n = new int[b.size];
newdata = new int[size];
for(int i= 0; i < size ; i++)
{
n[size-i]= data[size-i];
}
for(int i= 0; i < b.size ; i++)
{
m[size-i]= b.data[b.size-i];
}
for(int i = b.size-1; i>=0;i--) // this loop does the actual subtraction.
{
if(m[i] >= n[i])
{
newdata[i] = m[i] - n[i];
}
else
{
m[i-1] = m[i-1] - 1;
newdata[i] = (m[i] + 10) - n[i];
}
}
newdata[0] = newdata[0] * (-1);
}
return BigInt(newdata.length,newdata);
}

int main()
{
int s;
int *d;
cout<<"enter a size\n";
cin>> s;
char input[s];
cout<<"Enter a big integer.\n";
cin>> input;
d = new int[s];
for(int i = 0; i < s; i++)
{
d[i] = input[i];
}
BigInt *a = new BigInt(s,d);
}
Make sure to #include <fstream> when doing I/O operator overloads.

The second is an array, not an object. You can't query it for its length.
It looks like m is defined as one of
int m[ <some number> ]; int *m; int m[];

If the first, you can get the array's size with:
sizeof( m ) / sizeof( m[ 0 ] );
as in:
unsigned length = sizeof( m ) / sizeof( m[ 0 ] );

For the second two there is no way to learn the array's size without additional information.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.