Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error: expected initializer before '&' token

Reply
Thread Tools

error: expected initializer before '&' token

 
 
andrew browning
Guest
Posts: n/a
 
      03-13-2006
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;
}

 
Reply With Quote
 
 
 
 
Githlar
Guest
Posts: n/a
 
      03-13-2006
Are you prototyping your big_int type before you are defining your
functions? That might cause the problem.
 
Reply With Quote
 
 
 
 
andrew browning
Guest
Posts: n/a
 
      03-13-2006
big_int is the class type. also, my other overloaded operators are not
stricken with this error

 
Reply With Quote
 
Githlar
Guest
Posts: n/a
 
      03-13-2006
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?
 
Reply With Quote
 
andrew browning
Guest
Posts: n/a
 
      03-13-2006
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;
}

 
Reply With Quote
 
andrew browning
Guest
Posts: n/a
 
      03-13-2006
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:stream & operator <<
(std: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

 
Reply With Quote
 
Githlar
Guest
Posts: n/a
 
      03-13-2006
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
 
Reply With Quote
 
Githlar
Guest
Posts: n/a
 
      03-13-2006
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
 
Reply With Quote
 
andrew browning
Guest
Posts: n/a
 
      03-13-2006
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

 
Reply With Quote
 
andrew browning
Guest
Posts: n/a
 
      03-13-2006
correct... those are the lines where i get the error

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: expected initializer before <type> lovecreatesbea...@gmail.com C++ 1 05-15-2008 10:41 AM
This is an unexpected token. The expected token is 'NAME' =?Utf-8?B?Y2FzaGRlc2ttYWM=?= ASP .Net 2 07-13-2007 11:38 AM
Student requests help... expected `;' before '(' token sandy@murdocks.on.ca C++ 3 10-05-2006 03:24 AM
Header include problems error: expected class-name before '{' token bubzilla C Programming 2 09-25-2006 10:52 AM
expected unqualified-id before "." token? Thomas Barth C++ 3 07-18-2006 01:01 PM



Advertisments