error: expected initializer before '&' token

A

andrew browning

can someone please comment on what i may be doing wrong here? i am
overloading input and output operators and keep getting this error.
here are the code snipets, thanks

friend ostream & operator <<(ostream & outs, const big_int &
source){
outs << source.get_b1() << " " << source.get_b2();
return outs;
}

friend istream & operator >>(istream & ins, const big_int &
target){
ins >> target.big_int1 >> target.big_int2;
return ins;
}
 
G

Githlar

Are you prototyping your big_int type before you are defining your
functions? That might cause the problem.
 
A

andrew browning

big_int is the class type. also, my other overloaded operators are not
stricken with this error
 
G

Githlar

I know big_int is your class type, but is it defined or at least
prototyped before you declare your functions? These other functions you
speak of -- do you have an example?
 
A

andrew browning

i'm a novice so i will attempt to articulate the best i can. the
functions have indeed been prototyped and the variables big_int1 and
big_int2 have been declared as variables of the class type. however,
i'm not sure what you mean by prototyping big_int. here is an example
of another overloaded function

big_int operator / (const big_int& big_int1, const big_int&
big_int2){

return
big_int1 / big_int2;
}
 
A

andrew browning

perhaps this will simplify. below is the header and then the
implementation

#ifndef BIGINT_ABROWNIN
#define BIGINT_ABROWNIN
#include <iostream> //provides istream and ostream
#include <cstdlib> // provides size_t


namespace abrowning6 {

class BigInt {

public:

//TYPEDEFS and MEMBER CONSTANTS
typedef std::size_t size_type;
typedef int value_type;
static const size_type CAPACITY = 100;

//CONSTRUCTOR
BigInt ();

//MODIFICATION MEMBER FUNCTIONS
void insert(const value_type& entry);
void erase_all();

//CONSTANT MEMBER FUNCTIONS
size_type size() const { return used;}
bool is_item () const;
value_type current() const;
int getB1() const {return big_int1;}
int getB2() const {return big_int2;}
friend std::eek:stream & operator <<
(std::eek:stream & outs, const BigInt & source);
friend std::istream & operator >>
(std::istream & ins, const BigInt & target);
private:

value_type data[CAPACITY];
size_type used;
size_type current_index;
int sign;
int big_int1;
int big_int2;

};

// NONMEMBER FUNCTIONS for the big_int class
BigInt operator + (const BigInt& big_int1, const BigInt&
big_int2);
BigInt operator - (const BigInt& big_int1, const BigInt&
big_int2);
BigInt operator * (const BigInt& big_int1, const BigInt&
big_int2);
BigInt operator / (const BigInt& big_int1, const BigInt&
big_int2);
BigInt operator % (const BigInt& big_int1, const BigInt&
big_int2);



}
#endif //BIGINT_H




#include <cassert> //provides assert
#include "big_int.h"
#include <cstdlib>
#include <iostream>

namespace abrowning6 {

BigInt::BigInt()
:used(0),
current_index(0),
sign (+1),
big_int1(0),
big_int2(0)
{// Constructor has no work to do
}


const BigInt::size_type BigInt::CAPACITY;


void BigInt::insert(const value_type& entry){
assert (size() < CAPACITY);

data[used] = entry;
++ used;
}



void BigInt::erase_all(){
used = 0;
}


BigInt operator + (const BigInt& big_int1, const BigInt&
big_int2){

assert (big_int1.size() + big_int1.size() <= BigInt::CAPACITY);

return
big_int1 + big_int2;
}


BigInt operator - (const BigInt& big_int1, const BigInt& big_int2){

return
big_int1 - big_int2;
}

BigInt operator * (const BigInt& big_int1, const BigInt& big_int2){


assert (big_int1.size() * big_int2.size() <= BigInt::CAPACITY);

return
big_int1 * big_int2;

}





BigInt operator / (const BigInt& big_int1, const BigInt& big_int2){





return


big_int1 / big_int2;


}





BigInt operator % (const BigInt& big_int1, const BigInt&
big_int2){

BigInt modulus;





return


big_int1 % big_int2;


}





friend ostream & operator <<(ostream & outs, const BigInt&
big_int1){

outs << big_int1.get_b1() << " " << big_int1.get_b2();


return outs;


}











friend istream & operator >>(istream & ins, const BigInt&
big_int1){

ins >> target.big_int1 >> target.big_int2;


return ins;


}





}


1,5 Top
 
G

Githlar

Sorry, perhaps I wasn't clear enough. I can tell that you know what
prototyping means, so we'll skip that. What I really meant when I asked
if you had prototyped your class is if you had prototyped your class
ABOVE the function declarations (perhaps literally somewhere above in
the file or in a header file #include'd in a line above your function
declarations). If your class prototype or declaration is in a header
file it is also possible that you could have forgot to #include the
header file or you #include'd the header file too low in the file (which
isn't a common problem =P).

Another question I have for you is: Are all of your overloaded operator
functions in the same file one right after the other? I assume not
because some of the are friends and that last example appears to be a
member function.

Githlar
 
G

Githlar

That code helped a lot.

One problem I saw with the code is that there is no closing brace on the
line that says:

// Constructor has no work to do

And you're getting the errors on the lines that say:

friend ostream & operator <<(ostream & outs, const BigInt& big_int1){

and

friend istream & operator >>(istream & ins, const BigInt& big_int1){

right?

Githlar
 
A

andrew browning

yes, the class has been protoyped in a header file and i have double
checked to make sure the #include "BigInt.h" is present at the top of
the implementation and driver programs. the .h file is included at the
top of the other two files before any functions are defined or called.


all overload functions are in the same file one after another as in the
example provided
 
G

Githlar

That IS a strange error. I've never seen it before. One thing's for
sure, it's talking about the '&' next to the BigInt. I tried all kinds
of different things, but I couldn't figure it out. I guess this is where
I have to surrender this to the gurus =)

I do have one question that threw me for a loop. Why is it in your .cpp
file that you are able to use ostream and istream without the std::
prefix having not used a using directive (using namespace std;)?
 
A

Artie Gold

andrew said:
perhaps this will simplify. below is the header and then the
implementation [snip]

friend ostream & operator <<(ostream & outs, const BigInt&
big_int1){

ITYM:

std::eek:stream & operator <<(std::eek:stream & outs,
const BigInt& big_int1) {
outs << big_int1.get_b1() << " " << big_int1.get_b2();


return outs;


}


friend istream & operator >>(istream & ins, const BigInt&
big_int1){

ITYM:
std::istream & operator >>(std::istream & ins,
const BitInt& big_int1) {
ins >> target.big_int1 >> target.big_int2;


return ins;


}





}

HTH,
--ag
 
A

andrew browning

i wish i new... its my first time overloading input and output
operators. thanks ever so much for donating part of your sunday to
this.
 
A

Artie Gold

andrew said:
andrew said:
perhaps this will simplify. below is the header and then the
implementation
[snip]


friend ostream & operator <<(ostream & outs, const BigInt&
big_int1){


ITYM:

std::eek:stream & operator <<(std::eek:stream & outs,
const BigInt& big_int1) {
outs << big_int1.get_b1() << " " << big_int1.get_b2();


return outs;


}


friend istream & operator >>(istream & ins, const BigInt&
big_int1){


ITYM:
std::istream & operator >>(std::istream & ins,
const BitInt& big_int1) {
ins >> target.big_int1 >> target.big_int2;


return ins;


}





}
your right. now can you explain where my logic fell down?????
Please quote the appropriate amount of context. Fixed.

Elsethread, you seemed to think that you *could* use ostream and istream
without the `std::' in front of them -- but didn't know why. The answer
is that you couldn't, and can't (the error message *is* a confusing one).

The fact that `friend' is part of a class declaration, and not part of a
function signature was also pointed out elsethread.

HTH,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top