www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/13/00:25:14

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Inheritance, friends, and undefined symbol errors...
Date: Thu, 12 Jun 1997 08:37:19 -0700
Organization: Alcyone Systems
Lines: 51
Message-ID: <33A017AF.5B1C92DF@alcyone.com>
References: <3 DOT 0 DOT 1 DOT 32 DOT 19970608224719 DOT 00915100 AT mail DOT geocities DOT com>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mike Orbinpost wrote:
> 
> I am getting an Undefined symbol error when I build this project (in the
> linking actually).  outFile is defined as a virtual function in both the
> base and derived classes, it is implemented in the derived class but
> used
> in a friend function of the base.  From what I have read (in C++ Primer
> Plus by Waite Group Press) this should be a legitimate practice. 
> Anybody
> have any ideas.

The linker errors I get are:

/tmp/cca185292.o: In function `person::~person(void)':
/tmp/cca185292.o(.text+0xb8): undefined reference to
`Collectable::~Collectable(void)'
/tmp/cca185293.o: In function `person::person(char *)':
/tmp/cca185293.o(.text+0xd): undefined reference to
`Collectable::Collectable(void)'
/tmp/cca185293.o: In function `person::~person(void)':
/tmp/cca185293.o(.text+0x12c): undefined reference to
`Collectable::~Collectable(void)'

These are legitimate errors.  It's saying that it can't find
Collectable::Collectable and Collectable::~Collectable, which is correct,
because you haven't defined them.

If you're going to have a base class that will never be created itself,
then you should make it an abstract class.  You can make it abstract by
making any (but not necessarily all) of the virtual members pure, that is
by appending = 0 on the prototype.  Change the definition of Collectables
in testbase.h to this and it will work:

    class Collectable
    {
      public:
        Collectable() { } // this line can be left out
        virtual ~Collectable() { }

        virtual void outStream( ostream & os ) const = 0; // pure virtual

        friend ostream & operator<<(ostream & os, const Collectable & C );
    };

-- 
       Erik Max Francis, &tSftDotIotE / email / max AT alcyone DOT com
                     Alcyone Systems /   web / http://www.alcyone.com/max/
San Jose, California, United States /  icbm / 37 20 07 N  121 53 38 W
                                   \
     "Covenants without the sword / are but words."
                                 / Camden

- Raw text -


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