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 

Problem overloading <<

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





PostPosted: Fri Jul 29, 2005 3:19 pm    Post subject: Problem overloading << Reply with quote



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





PostPosted: Fri Jul 29, 2005 3:33 pm    Post subject: Re: Problem overloading << Reply with 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;
}

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





PostPosted: Mon Aug 01, 2005 12:35 pm    Post subject: Re: Problem overloading << Reply with quote



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