error: expected initializer before '&' token

Discussion in 'C++' started by andrew browning, Mar 13, 2006.

  1. 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;
    }
    andrew browning, Mar 13, 2006
    #1
    1. Advertising

  2. andrew browning

    Githlar Guest

    Are you prototyping your big_int type before you are defining your
    functions? That might cause the problem.
    Githlar, Mar 13, 2006
    #2
    1. Advertising

  3. big_int is the class type. also, my other overloaded operators are not
    stricken with this error
    andrew browning, Mar 13, 2006
    #3
  4. andrew browning

    Githlar Guest

    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?
    Githlar, Mar 13, 2006
    #4
  5. 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;
    }
    andrew browning, Mar 13, 2006
    #5
  6. 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
    andrew browning, Mar 13, 2006
    #6
  7. andrew browning

    Githlar Guest

    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
    Githlar, Mar 13, 2006
    #7
  8. andrew browning

    Githlar Guest

    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
    Githlar, Mar 13, 2006
    #8
  9. 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
    andrew browning, Mar 13, 2006
    #9
  10. correct... those are the lines where i get the error
    andrew browning, Mar 13, 2006
    #10
  11. closing brace is there... still get error
    andrew browning, Mar 13, 2006
    #11
  12. andrew browning

    red floyd Guest

    andrew browning wrote:
    > correct... those are the lines where i get the error
    >


    You can't use the keyword "friend" outside a class definition.
    red floyd, Mar 13, 2006
    #12
  13. andrew browning

    Githlar Guest

    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;)?
    Githlar, Mar 13, 2006
    #13
  14. andrew browning

    Artie Gold Guest

    andrew browning wrote:
    > 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

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    "You can't KISS* unless you MISS**"
    [*-Keep it simple, stupid. **-Make it simple, stupid.]
    Artie Gold, Mar 13, 2006
    #14
  15. 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.
    andrew browning, Mar 13, 2006
    #15
  16. your right. now can you explain where my logic fell down?????
    andrew browning, Mar 13, 2006
    #16
  17. andrew browning

    Artie Gold Guest

    andrew browning wrote:
    > andrew browning wrote:
    >
    >> 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

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    "You can't KISS* unless you MISS**"
    [*-Keep it simple, stupid. **-Make it simple, stupid.]
    Artie Gold, Mar 13, 2006
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bubzilla
    Replies:
    2
    Views:
    914
    bubzilla
    Sep 25, 2006
  2. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    This is an unexpected token. The expected token is 'NAME'

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Jul 13, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    771
    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=
    Jul 13, 2007
  3. Ram  Prasad
    Replies:
    21
    Views:
    2,811
    Kenneth Brody
    Jul 31, 2007
  4. Replies:
    5
    Views:
    23,667
  5. Replies:
    1
    Views:
    19,511
Loading...

Share This Page