 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
zeroYouMustNotSpamtype@ya Guest
|
Posted: Fri Jul 29, 2005 3:19 pm Post subject: Problem overloading << |
|
|
Hi,
Describing this problem will be a bit long winded, but please bear
with me:
I've got three files in my project: permuts.h, permuts.cpp, and
braids.cpp (some content from wich will eventually be moved into
braids.h). Both cpp files include the h file. Permuts.h contains the
Permutation class, for which I needed to overload <<. Doing this in
that file caused an error(Duplicate definition I think - it occurs to
me now to try #ifndef on this) so I moved it to permuts.cpp. But it
wouldn't compile (The error message from GNU C++ was HUGE!) unless I
deleted it from permuts and put it in braids (which also housed main()
- and no, it couldn't ccompile when the overloaded << was in both CPP
files.)
Can anyone explain why it won't go in permuts.cpp (even if the answer
is I should have used #ifndef and put it in the .h)
Thanks,
James McLaughlin.
|
|
| Back to top |
|
 |
Earl Purple Guest
|
Posted: Fri Jul 29, 2005 3:33 pm Post subject: Re: Problem overloading << |
|
|
Why not declare the function (as a prototype) in the header file
(permuts.h) and define it in permuts.cpp. Thus:
// in permuts.h:
std::ostream & operator<<( ostream &, const Permutation & );
// in permuts.cpp
std::ostream & operator<<( ostream & os, const Permutation & perm)
{
// body here
return os;
}
If you want this function to access private members of Permutation,
thus be a friend, then Permutation.h should look like:
// in permuts.h:
#ifndef PERMUTATION_INCLUDED
#define PERMUTATION_INCLUDED
class Permutation; // forwardly declare Permutation
std::ostream & operator<<( ostream &, const Permutation & );
class Permutation
{
friend std::ostream & operator<<( ostream &, const Permutation & );
// class definition here
};
#endif
|
|
| Back to top |
|
 |
zerWeDoNotSendTheSpamToMe Guest
|
Posted: Mon Aug 01, 2005 12:35 pm Post subject: Re: Problem overloading << |
|
|
Thanks Earl, your suggestion solved the problem!
On 29 Jul 2005 08:33:28 -0700, "Earl Purple" <earlpurple0 (AT) yahoo (DOT) com>
wrotE:
| Quote: | Why not declare the function (as a prototype) in the header file
(permuts.h) and define it in permuts.cpp. Thus:
// in permuts.h:
std::ostream & operator<<( ostream &, const Permutation & );
// in permuts.cpp
std::ostream & operator<<( ostream & os, const Permutation & perm)
{
// body here
return os;
}
|
|
|
| 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
|
|