multiply defined operator

Discussion in 'C++' started by Brett Irving, Jun 29, 2003.

  1. Brett Irving

    Brett Irving Guest

    Hi I keep having problems with trying to overload the << operator,
    the compiler will not let me define it.

    the code looks like

    #ifndef __MYARRAY_H__
    #define __MYARRAY_H__
    #include <iostream>
    ..
    ..
    ..
    ..
    };

    ostream & operator<<(ostream &o, MyArray& m)
    {
    return m(o);
    }

    #endif

    It wont work if I put it inside or out of the #endif

    The error that the compiler keeps giving me is this.

    ld: fatal: symbol `operator<<(std::basic_ostream<char,
    std::char_traits<char> >&, MyArray&)' is multiply defined:
    (file MyArrayMain.o and file MyArray.o);
    ld: fatal: File processing errors. No output written to myarray
    collect2: ld returned 1 exit status



    Please help.
    thanks a lot
    Brett Irving, Jun 29, 2003
    #1
    1. Advertising

  2. "Brett Irving" <> wrote in message
    news:...
    > Hi I keep having problems with trying to overload the << operator,
    > the compiler will not let me define it.
    >
    > the code looks like
    >
    > #ifndef __MYARRAY_H__
    > #define __MYARRAY_H__
    > #include <iostream>
    > .
    > .
    > .
    > .
    > };
    >
    > ostream & operator<<(ostream &o, MyArray& m)


    Should be

    inline ostream & operator<<(ostream &o, MyArray& m)

    Also you are ignoring the issue of const, you should write

    inline ostream & operator<<(ostream &o, const MyArray& m)

    because you don't change a MyArray when you output it. Although making this
    change will probably mean that you have to stop ignoring the issue of const
    in the declaration of MyArray as well. Look on it as a good oppurtunity to
    learn about const.

    john
    John Harrison, Jun 29, 2003
    #2
    1. Advertising

  3. Brett Irving

    Aggro Guest

    Brett Irving wrote:

    > #ifndef __MYARRAY_H__
    > #define __MYARRAY_H__


    C++ standard specifically reserves macros with two sequential
    underscores for use by the implementation. In most cases, there is no
    problem, but if you want your code to be portable... You will propably
    do just fine if you write only:

    #ifndef MYARRAY_H
    #define MYARRAY_H
    Aggro, Jun 29, 2003
    #3
  4. Brett Irving

    Brett Irving Guest

    Thanks Heaps



    "John Harrison" <> wrote in message news:<bdm2lt$tt73n$>...
    > "Brett Irving" <> wrote in message
    > news:...
    > > Hi I keep having problems with trying to overload the << operator,
    > > the compiler will not let me define it.
    > >
    > > the code looks like
    > >
    > > #ifndef __MYARRAY_H__
    > > #define __MYARRAY_H__
    > > #include <iostream>
    > > .
    > > .
    > > .
    > > .
    > > };
    > >
    > > ostream & operator<<(ostream &o, MyArray& m)

    >
    > Should be
    >
    > inline ostream & operator<<(ostream &o, MyArray& m)
    >
    > Also you are ignoring the issue of const, you should write
    >
    > inline ostream & operator<<(ostream &o, const MyArray& m)
    >
    > because you don't change a MyArray when you output it. Although making this
    > change will probably mean that you have to stop ignoring the issue of const
    > in the declaration of MyArray as well. Look on it as a good oppurtunity to
    > learn about const.
    >
    > john
    Brett Irving, Jun 29, 2003
    #4
    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. Oodini
    Replies:
    1
    Views:
    1,754
    Keith Thompson
    Sep 27, 2005
  2. Dave
    Replies:
    3
    Views:
    488
    Alipha
    Aug 9, 2005
  3. Angel Tsankov
    Replies:
    1
    Views:
    857
    Victor Bazarov
    Apr 5, 2006
  4. ADS7328

    multiply-defined problem

    ADS7328, Jun 21, 2007, in forum: C++
    Replies:
    3
    Views:
    1,016
    Victor Bazarov
    Jun 21, 2007
  5. hurcan solter
    Replies:
    3
    Views:
    716
    Cholo Lennon
    Aug 29, 2007
Loading...

Share This Page