473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Jul 29 '05 #1
2 1291
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

Jul 29 '05 #2
Thanks Earl, your suggestion solved the problem!

On 29 Jul 2005 08:33:28 -0700, "Earl Purple" <ea*********@yahoo.com>
wrotE:
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;
}

Aug 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Philipp Lenssen | last post by:
My friend has the following problem (background: we want to transform XML to XHTML via XSLT): "We copy XHTML fragments into an output by using the following template: <xsl:template match="*"...
9
by: tomakated | last post by:
How do I get rid of the dashes next to the 3, 6, and the 9<?. Also the last row of "------" and the "|" on the last line so that the board will look like this<?. | | | | 1| 2| ...
10
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
3
by: pmatos | last post by:
Hi all, I'm having a problem and for illustration purposes I developed code that shows what the problem is about. However, any comment on the code which is not directly about this issue is...
1
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is...
3
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion....
3
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I...
8
by: Micko1 | last post by:
hi there, I have an issue when overloading <<. string operator << (room * &currentPlayerLocation); string room::operator << (room * &currentPlayerLocation) {
2
by: NannMaw | last post by:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Server Error in '/WebSite1' Application. -------------------------------------------------------------------------------- Invalid postback or callback argument. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.