multiply defined operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Irving

    multiply defined operator

    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<<(ostr eam &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_trait s<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
  • John Harrison

    #2
    Re: multiply defined operator


    "Brett Irving" <balgorg@hotmai l.com> wrote in message
    news:4f68b81.03 06282243.2133fa 8e@posting.goog le.com...[color=blue]
    > 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<<(ostr eam &o, MyArray& m)[/color]

    Should be

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

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

    inline ostream & operator<<(ostr eam &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


    Comment

    • Aggro

      #3
      Re: multiply defined operator

      Brett Irving wrote:
      [color=blue]
      > #ifndef __MYARRAY_H__
      > #define __MYARRAY_H__[/color]

      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

      Comment

      • Brett Irving

        #4
        Re: multiply defined operator

        Thanks Heaps



        "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<bdm2lt$tt 73n$1@ID-196037.news.dfn cis.de>...[color=blue]
        > "Brett Irving" <balgorg@hotmai l.com> wrote in message
        > news:4f68b81.03 06282243.2133fa 8e@posting.goog le.com...[color=green]
        > > 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<<(ostr eam &o, MyArray& m)[/color]
        >
        > Should be
        >
        > inline ostream & operator<<(ostr eam &o, MyArray& m)
        >
        > Also you are ignoring the issue of const, you should write
        >
        > inline ostream & operator<<(ostr eam &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[/color]

        Comment

        Working...