Errors overloading the << operator.

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

class BigInt{

private:
int* data;
int size;

public:
BigInt::BigInt(int s, int d[]);
BigInt BigInt::operator+(const BigInt& b);
// BigInt BigInt::operator*(const BigInt& b);
BigInt BigInt::operator-(const BigInt& b);

bool BigInt::operator<(const BigInt& b);
bool BigInt::operator>(const BigInt& b);

}
std::ostream& BigInt::operator<<(std::ostream& outs, BigInt& a);
#endif /* _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);
    ...
  };


Hope this helps.
for future reference how do i use code tags?
Click the big "Insert Code" button and stick your code between the tags.

Were you able to solve your problem?
Last edited on
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"

they all exist on these two lines

line27 from .h
 
std::ostream& friend operator<<(std::ostream& outs, const BigInt& a);


line37 from .cc
 
ostream& friend BigInt::operator<<(ostream& outs,const BigInt& a)


here is the whole header now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef _BIGINT_H
#define	_BIGINT_H

class BigInt{

private:
    int* data;
    int size;
    
public:
    BigInt(int s, int d[]);
    BigInt operator+(const BigInt& b);
//    BigInt operator*(const BigInt& b);
    BigInt operator-(const BigInt& b);
    
    bool operator<(const BigInt& b);
    bool operator>(const BigInt& b);
    
}
std::ostream& friend operator<<(std::ostream& outs, const BigInt& a);
#endif	/* _BIGINT_H */ 


and my other code with some functions removed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "BigInt.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std; 

ostream& friend BigInt::operator<<(ostream& outs,const 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);
}
Ahh, you need to include the appropriate files:
1
2
3
4
5
6
7
#ifndef _BIGINT_H
#define	_BIGINT_H

#include <iostream>

class BigInt{
...


It is complaining because it doesn't know what "ostream" is.

Hope this helps.
Last edited on
ya i included that and it didn't change anything. got any other ideas?
You included it in your BigInt.h file like I showed you?
Topic archived. No new replies allowed.