Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multiply defined operator

Reply
Thread Tools

multiply defined operator

 
 
Brett Irving
Guest
Posts: n/a
 
      06-29-2003
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-29-2003

"Brett Irving" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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


 
Reply With Quote
 
 
 
 
Aggro
Guest
Posts: n/a
 
      06-29-2003
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

 
Reply With Quote
 
Brett Irving
Guest
Posts: n/a
 
      06-29-2003
Thanks Heaps



"John Harrison" <(E-Mail Removed)> wrote in message news:<bdm2lt$tt73n$(E-Mail Removed)>...
> "Brett Irving" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > 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

 
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
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
multiply-defined problem ADS7328 C++ 3 06-21-2007 02:51 PM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Avoiding multiply defined symbols for member functions Dave C++ 3 08-09-2005 03:09 AM



Advertisments