error: expected initializer before '&' token

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew browning

    error: expected initializer before '&' token

    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;
    }

  • Githlar

    #2
    Re: error: expected initializer before '&amp;' token

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

    Comment

    • andrew browning

      #3
      Re: error: expected initializer before '&amp;' token

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

      Comment

      • Githlar

        #4
        Re: error: expected initializer before '&amp;' token

        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?

        Comment

        • andrew browning

          #5
          Re: error: expected initializer before '&amp;' token

          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;
          }

          Comment

          • andrew browning

            #6
            Re: error: expected initializer before '&amp;' token

            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::ostream & operator <<
            (std::ostream & 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_ty pe BigInt::CAPACIT Y;


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

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



            void BigInt::erase_a ll(){
            used = 0;
            }


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

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

            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::CAPACIT Y);

            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

            Comment

            • Githlar

              #7
              Re: error: expected initializer before '&amp;' token

              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

              Comment

              • Githlar

                #8
                Re: error: expected initializer before '&amp;' token

                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

                Comment

                • andrew browning

                  #9
                  Re: error: expected initializer before '&amp;' token

                  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

                  Comment

                  • andrew browning

                    #10
                    Re: error: expected initializer before '&amp;' token

                    correct... those are the lines where i get the error

                    Comment

                    • andrew browning

                      #11
                      Re: error: expected initializer before '&amp;' token

                      closing brace is there... still get error

                      Comment

                      • red floyd

                        #12
                        Re: error: expected initializer before '&amp;' token

                        andrew browning wrote:[color=blue]
                        > correct... those are the lines where i get the error
                        >[/color]

                        You can't use the keyword "friend" outside a class definition.

                        Comment

                        • Githlar

                          #13
                          Re: error: expected initializer before '&amp;' token

                          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;)?

                          Comment

                          • Artie Gold

                            #14
                            Re: error: expected initializer before '&amp;' token

                            andrew browning wrote:[color=blue]
                            > perhaps this will simplify. below is the header and then the
                            > implementation[/color]
                            [snip][color=blue]
                            >
                            > friend ostream & operator <<(ostream & outs, const BigInt&
                            > big_int1){[/color]

                            ITYM:

                            std::ostream & operator <<(std::ostre am & outs,
                            const BigInt& big_int1) {[color=blue]
                            >
                            > outs << big_int1.get_b1 () << " " << big_int1.get_b2 ();
                            >
                            >
                            > return outs;
                            >
                            >
                            > }
                            >
                            >
                            > friend istream & operator >>(istream & ins, const BigInt&
                            > big_int1){[/color]

                            ITYM:
                            std::istream & operator >>(std::istre am & ins,
                            const BitInt& big_int1) {[color=blue]
                            >
                            > ins >> target.big_int1 >> target.big_int2 ;
                            >
                            >
                            > return ins;
                            >
                            >
                            > }
                            >
                            >
                            >
                            >
                            >
                            > }
                            >
                            >[/color]

                            HTH,
                            --ag

                            --
                            Artie Gold -- Austin, Texas
                            I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

                            "You can't KISS* unless you MISS**"
                            [*-Keep it simple, stupid. **-Make it simple, stupid.]

                            Comment

                            • andrew browning

                              #15
                              Re: error: expected initializer before '&amp;' token

                              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.

                              Comment

                              Working...