 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Brett Irving Guest
|
Posted: Sun Jun 29, 2003 6:43 am Post subject: 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
..
..
..
..
};
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
|
Posted: Sun Jun 29, 2003 6:58 am Post subject: Re: multiply defined operator |
|
|
"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
|
Posted: Sun Jun 29, 2003 7:07 am Post subject: Re: multiply defined operator |
|
|
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
|
Posted: Sun Jun 29, 2003 9:05 am Post subject: Re: multiply defined operator |
|
|
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 |
|
 |
|
|
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
|
|