www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/15/18:45:21

From: Steve Chapel <schapel AT utilicom DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Linking problems with overloading operator<<
Date: Thu, 15 Oct 1998 15:16:05 -0700
Organization: Utilicom, Inc.
Lines: 81
Message-ID: <36267425.718995BD@utilicom.com>
References: <362651bd DOT 2990955 AT news DOT usit DOT net>
NNTP-Posting-Host: steve.utilicom.com
Mime-Version: 1.0
X-Trace: sienna.impulse.net 908489767 11694 207.33.43.138 (15 Oct 1998 22:16:07 GMT)
X-Complaints-To: abuse AT impulse DOT net
NNTP-Posting-Date: 15 Oct 1998 22:16:07 GMT
X-Mailer: Mozilla 4.5b2 [en] (Win95; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

This is a multi-part message in MIME format.
--------------EB1DBE1FD17267E2E4A19A66
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

gamma wrote:
> 
> I am having problems linking files that have the insertion and
> extraction operators overloaded.... here is a little snippet of
> code...
> 
> //Golfer.h
> 
> #ifndef _GOLFER_H_
> #define _GOLFER_H_
> 
> #include <string>
> #include <iostream>
> using namespace std;
> 
> class Golfer
> {
>         friend ostream& operator<<(ostream&, const Golfer&);
>         friend istream& operator>>(istream&, Golfer&);
> .
> .
> .
> }
> 
> ostream& operator<<(ostream& gout, const Golfer& outGolfer)
> {
>         gout << outGolfer.PIT << "\t" << outGolfer.name << "\t" <<
> outGolfer.rank;
>         return gout;
> }
> .
> .
> .
> 
> #endif
> 
> This complies okay but returns these linker errors.
> 
> Error:  Golfer.o :  In fraction 'operator<<(ostream &, Golfer const
> &)':
> Error:  Golfer.cpp(.text+0x4): multiple definition of
> 'operator<<(ostream &, Golfer const &)'
> 
> Any help would be greatly appreciated
> Thanks
> Charlie Mac.

The line with "friend" in the class is a declaration. The code where you
supply the details of the function is a definition.
You want to put only declarations in header files and definitions in
.cpp files.
--------------EB1DBE1FD17267E2E4A19A66
Content-Type: text/x-vcard; charset=us-ascii;
 name="schapel.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Steve Chapel
Content-Disposition: attachment;
 filename="schapel.vcf"

begin:vcard
n:Chapel;Steve
tel;fax:(805) 964-5706
tel;work:(805) 964-5848 x227
x-mozilla-html:TRUE
url:http://www.utilicom.com/
org:Utilicom, Inc. Spread Spectrum Communications
adr:;;323 Love Place;Santa Barbara;CA;93117-3289;USA
version:2.1
email;internet:schapel AT utilicom DOT com
title:Software Engineer
fn:Steve Chapel
end:vcard


--------------EB1DBE1FD17267E2E4A19A66--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019