I got the following errors from my code, and im not sure why any suggestions would be nice and i dont need the super technical explanation why but if you know why mine isn't working id like to know. I don't need to make the operator<< function a friend function do I.
In file included from BigInt.cc:2:
BigInt.h:27: error: expected init-declarator before '&' token
BigInt.h:27: error: expected `,' or `;' before '&' token
BigInt.cc:37: error: `std::ostream& BigInt::operator<<(std::ostream&, BigInt&)' must take exactly one argument
BigInt.cc:37: error: no `std::ostream& BigInt::operator<<(std::ostream&, BigInt&)' member function declared in class `BigInt'
BigInt.cc: In member function `std::ostream& BigInt::operator<<(std::ostream&, BigInt&)':
BigInt.cc:38: error: expected `;' before ')' token
here is the code i left out the implementation of some methods that are unrelated since they are fairly lengthy.
// this is my header file
#ifndef _BIGINT_H
#define _BIGINT_H
// this is my implementation file
#include <iostream>
#include "BigInt.h"
#include <stdlib.h>
#include <stdio.h>
using namespace std;
// i ommitted the implementation for all the other methods but i had them all
// here before the operator<< method.
ostream& BigInt::operator<<(ostream& outs, BigInt& a)
{
for(int i = 0; i < a.size,i++)
{
outs<<a.data[i];
}
return outs;
}
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);
}
Please use code tags. I can't diagnose your problems because the line numbers listed are beyond the EOF for the .h file you posted.
In the << overload, you accidentally put a semicolon (;) in the for statement instead of a comma (,).
The prototype should be: std::ostream& operator << (std::ostream& outs, BigInt& a)
and yes, it needs to be a friend of the BigInt class.
In the .h file, you don't need to use BigInt:: before class names. That is implied.
1 2 3 4 5 6 7
class BigInt {
...
public:
BigInt(int s, int d[]);
BigInt operator+(const BigInt& b);
...
};
nope, made some changes to the header to remove the scope operator. but is still have the following errors
In file included from BigInt.cc:2:
BigInt.h:27: error: expected init-declarator before '&' token
BigInt.h:27: error: expected `,' or `;' before '&' token
BigInt.cc:37: error: expected unqualified-id before "friend"
BigInt.cc:37: error: expected init-declarator before "friend"
BigInt.cc:37: error: expected `,' or `;' before "friend"