C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

multiply defined operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Brett Irving
Guest





PostPosted: Sun Jun 29, 2003 6:43 am    Post subject: multiply defined operator Reply with quote



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

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 std::char_traits&, 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
Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 6:58 am    Post subject: Re: multiply defined operator Reply with quote




"Brett Irving" <balgorg (AT) hotmail (DOT) com> wrote

Quote:
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 .
.
.
.
};

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



Back to top
Aggro
Guest





PostPosted: Sun Jun 29, 2003 7:07 am    Post subject: Re: multiply defined operator Reply with quote



Brett Irving wrote:

Quote:
#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


Back to top
Brett Irving
Guest





PostPosted: Sun Jun 29, 2003 9:05 am    Post subject: Re: multiply defined operator Reply with quote

Thanks Heaps



"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:
"Brett Irving" <balgorg (AT) hotmail (DOT) com> wrote in message
news:4f68b81.0306282243.2133fa8e (AT) posting (DOT) google.com...
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 .
.
.
.
};

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.