Plz help- "undefined reference" error in short sample c++ program

Plz help- "undefined reference" error in short sample c++ program

Post by Joseph Thom » Tue, 11 Dec 2001 15:10:52



When I compile the following program (taken directly from the C++ FAQ
book), I get undefined reference errors for an element in the
program's defined class.
I'd read that uninstalling/reinstalling c++ libs fixes the problem.
However, I've done this (and have the latest version of gcc and libs),
to no effect.  Currently using RedHat 7.1.

Can anyone help?

Thanks,
Joe

cc recycle.cpp
/tmp/cco69TPH.o: In function `Fred::create(void)':
/tmp/cco69TPH.o(.text+0x69): undefined reference to
`Fred::headRecycled_'
/tmp/cco69TPH.o(.text+0xb9): undefined reference to
`Fred::headRecycled_'
/tmp/cco69TPH.o(.text+0xc1): undefined reference to
`Fred::headRecycled_'
/tmp/cco69TPH.o(.text+0xc8): undefined reference to
`Fred::headRecycled_'
/tmp/cco69TPH.o: In function `Fred::discard(void)':
/tmp/cco69TPH.o(.text+0x14e): undefined reference to
`Fred::headRecycled_'
/tmp/cco69TPH.o(.text+0x156): more undefined references to
`Fred::headRecycled_' follow
collect2: ld returned 1 exit status

==========================================
#include <new>
using namespace std;

class Fred {
public:
  static Fred* create() throw(bad_alloc);
  virtual void discard() throw();
  static bool flushPool() throw();
private:
  Fred() throw();
  ~Fred() throw();
  static Fred* headRecycled_;
  void init() throw();
  Fred* nextRecycled_;

Quote:};

void Fred::init() throw()
{
  //
  nextRecycled_ = NULL;

Quote:}

Fred::Fred() throw()
{ init(); }

Fred::~Fred() throw()
{ }

Fred* Fred::create() throw(bad_alloc)
{
  if (Fred::headRecycled_ == NULL)
    return new Fred();
  Fred* p = Fred::headRecycled_;
  Fred::headRecycled_ = Fred::headRecycled_->nextRecycled_;
  p->init();
  return p;

Quote:}

void Fred::discard() throw()
{
  nextRecycled_ = headRecycled_;
  headRecycled_  = this;

Quote:}

bool Fred::flushPool() throw()
{
  bool stuffGotDeleted = (headRecycled_ != NULL);
  while (headRecycled_ != NULL)
    delete create();
  return stuffGotDeleted;

Quote:}

void flushAllPools() throw(bad_alloc)
{
  unsigned n = 0;
  n += Fred::flushPool();

  if (n == 0)
    throw bad_alloc();

Quote:}

int main()
{
  set_new_handler(flushAllPools);
Quote:}

========================================
 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by ken_yap_3160cfa7_.. » Tue, 11 Dec 2001 15:15:48


|cc recycle.cpp
|/tmp/cco69TPH.o: In function `Fred::create(void)':
|/tmp/cco69TPH.o(.text+0x69): undefined reference to
|`Fred::headRecycled_'

Try compiling with g++ or c++, not cc.

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Dominik Sache » Tue, 11 Dec 2001 19:31:15


try adding this to your code:

static Fred* Fred::headRecycled_=0;

you have to initialize static member-variables outside the class containing
them.
regards,
dominik



Quote:> When I compile the following program (taken directly from the C++ FAQ
> book), I get undefined reference errors for an element in the
> program's defined class.
> I'd read that uninstalling/reinstalling c++ libs fixes the problem.
> However, I've done this (and have the latest version of gcc and libs),
> to no effect.  Currently using RedHat 7.1.

> Can anyone help?

> Thanks,
> Joe

> cc recycle.cpp
> /tmp/cco69TPH.o: In function `Fred::create(void)':
> /tmp/cco69TPH.o(.text+0x69): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0xb9): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0xc1): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0xc8): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o: In function `Fred::discard(void)':
> /tmp/cco69TPH.o(.text+0x14e): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0x156): more undefined references to
> `Fred::headRecycled_' follow
> collect2: ld returned 1 exit status

> ==========================================
> #include <new>
> using namespace std;

> class Fred {
> public:
>   static Fred* create() throw(bad_alloc);
>   virtual void discard() throw();
>   static bool flushPool() throw();
> private:
>   Fred() throw();
>   ~Fred() throw();
>   static Fred* headRecycled_;
>   void init() throw();
>   Fred* nextRecycled_;
> };

> void Fred::init() throw()
> {
>   //
>   nextRecycled_ = NULL;
> }

> Fred::Fred() throw()
> { init(); }

> Fred::~Fred() throw()
> { }

> Fred* Fred::create() throw(bad_alloc)
> {
>   if (Fred::headRecycled_ == NULL)
>     return new Fred();
>   Fred* p = Fred::headRecycled_;
>   Fred::headRecycled_ = Fred::headRecycled_->nextRecycled_;
>   p->init();
>   return p;
> }

> void Fred::discard() throw()
> {
>   nextRecycled_ = headRecycled_;
>   headRecycled_  = this;
> }

> bool Fred::flushPool() throw()
> {
>   bool stuffGotDeleted = (headRecycled_ != NULL);
>   while (headRecycled_ != NULL)
>     delete create();
>   return stuffGotDeleted;
> }

> void flushAllPools() throw(bad_alloc)
> {
>   unsigned n = 0;
>   n += Fred::flushPool();

>   if (n == 0)
>     throw bad_alloc();
> }

> int main()
> {
>   set_new_handler(flushAllPools);
> }
> ========================================

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Eugene Kuznetso » Tue, 11 Dec 2001 21:07:25



> When I compile the following program (taken directly from the C++ FAQ
> book), I get undefined reference errors for an element in the
> program's defined class.
> ==========================================
> #include <new>
> using namespace std;

> class Fred {
> public:
>   static Fred* create() throw(bad_alloc);
>   virtual void discard() throw();
>   static bool flushPool() throw();
> private:
>   Fred() throw();
>   ~Fred() throw();
>   static Fred* headRecycled_;
>   void init() throw();
>   Fred* nextRecycled_;
> };

A definition of Fred::headRecycled_ is missing. If you took it from a book,
double-check the code and if it's still not there, consider getting a
different book.

To compile it, you want to add this outside the class declaration:

Fred* Fred::headRecycled_;

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by jack le » Wed, 12 Dec 2001 00:01:31



> When I compile the following program (taken directly from the C++ FAQ
> book), I get undefined reference errors for an element in the
> program's defined class.
> I'd read that uninstalling/reinstalling c++ libs fixes the problem.
> However, I've done this (and have the latest version of gcc and libs),
> to no effect.  Currently using RedHat 7.1.

> Can anyone help?

> Thanks,
> Joe

> cc recycle.cpp
> /tmp/cco69TPH.o: In function `Fred::create(void)':
> /tmp/cco69TPH.o(.text+0x69): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0xb9): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0xc1): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0xc8): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o: In function `Fred::discard(void)':
> /tmp/cco69TPH.o(.text+0x14e): undefined reference to
> `Fred::headRecycled_'
> /tmp/cco69TPH.o(.text+0x156): more undefined references to
> `Fred::headRecycled_' follow
> collect2: ld returned 1 exit status

> ==========================================
> #include <new>
> using namespace std;

> class Fred {
> public:
>   static Fred* create() throw(bad_alloc);
>   virtual void discard() throw();
>   static bool flushPool() throw();
> private:
>   Fred() throw();
>   ~Fred() throw();
>   static Fred* headRecycled_;
>   void init() throw();
>   Fred* nextRecycled_;
> };

> void Fred::init() throw()
> {
>   //
>   nextRecycled_ = NULL;
> }

> Fred::Fred() throw()
> { init(); }

> Fred::~Fred() throw()
> { }

> Fred* Fred::create() throw(bad_alloc)
> {
>   if (Fred::headRecycled_ == NULL)
>     return new Fred();
>   Fred* p = Fred::headRecycled_;
>   Fred::headRecycled_ = Fred::headRecycled_->nextRecycled_;
>   p->init();
>   return p;
> }

> void Fred::discard() throw()
> {
>   nextRecycled_ = headRecycled_;
>   headRecycled_  = this;
> }

> bool Fred::flushPool() throw()
> {
>   bool stuffGotDeleted = (headRecycled_ != NULL);
>   while (headRecycled_ != NULL)
>     delete create();
>   return stuffGotDeleted;
> }

> void flushAllPools() throw(bad_alloc)
> {
>   unsigned n = 0;
>   n += Fred::flushPool();

>   if (n == 0)
>     throw bad_alloc();
> }

> int main()
> {
>   set_new_handler(flushAllPools);
> }
> ========================================

You must include a definition for your static member
headRecycled_ in your code,

Fred *Fred::headRecycled_;

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Donovan Rebbech » Wed, 12 Dec 2001 00:58:25



> When I compile the following program (taken directly from the C++ FAQ
> book), I get undefined reference errors for an element in the
> program's defined class.
> I'd read that uninstalling/reinstalling c++ libs fixes the problem.
> However, I've done this (and have the latest version of gcc and libs),
> to no effect.  Currently using RedHat 7.1.

> Can anyone help?

You can't just declare a static member, you need a definition.
eg:

class Foo
{
        static int x;

Quote:};

int Foo::x = 0;

--
Donovan

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Joseph Thom » Wed, 12 Dec 2001 01:00:46


Thanks for your reply.
I tried c++ and g++, but same results.  Does this seem like a linker
problem, or is it just something wrong with the sample program?

Thanks,
Joe


> try adding this to your code:

> static Fred* Fred::headRecycled_=0;

> you have to initialize static member-variables outside the class containing
> them.
> regards,
> dominik



> > When I compile the following program (taken directly from the C++ FAQ
> > book), I get undefined reference errors for an element in the
> > program's defined class.
> > I'd read that uninstalling/reinstalling c++ libs fixes the problem.
> > However, I've done this (and have the latest version of gcc and libs),
> > to no effect.  Currently using RedHat 7.1.

> > Can anyone help?

> > Thanks,
> > Joe

> > cc recycle.cpp
> > /tmp/cco69TPH.o: In function `Fred::create(void)':
> > /tmp/cco69TPH.o(.text+0x69): undefined reference to
> > `Fred::headRecycled_'
> > /tmp/cco69TPH.o(.text+0xb9): undefined reference to
> > `Fred::headRecycled_'
> > /tmp/cco69TPH.o(.text+0xc1): undefined reference to
> > `Fred::headRecycled_'
> > /tmp/cco69TPH.o(.text+0xc8): undefined reference to
> > `Fred::headRecycled_'
> > /tmp/cco69TPH.o: In function `Fred::discard(void)':
> > /tmp/cco69TPH.o(.text+0x14e): undefined reference to
> > `Fred::headRecycled_'
> > /tmp/cco69TPH.o(.text+0x156): more undefined references to
> > `Fred::headRecycled_' follow
> > collect2: ld returned 1 exit status

> > ==========================================
> > #include <new>
> > using namespace std;

> > class Fred {
> > public:
> >   static Fred* create() throw(bad_alloc);
> >   virtual void discard() throw();
> >   static bool flushPool() throw();
> > private:
> >   Fred() throw();
> >   ~Fred() throw();
> >   static Fred* headRecycled_;
> >   void init() throw();
> >   Fred* nextRecycled_;
> > };

> > void Fred::init() throw()
> > {
> >   //
> >   nextRecycled_ = NULL;
> > }

> > Fred::Fred() throw()
> > { init(); }

> > Fred::~Fred() throw()
> > { }

> > Fred* Fred::create() throw(bad_alloc)
> > {
> >   if (Fred::headRecycled_ == NULL)
> >     return new Fred();
> >   Fred* p = Fred::headRecycled_;
> >   Fred::headRecycled_ = Fred::headRecycled_->nextRecycled_;
> >   p->init();
> >   return p;
> > }

> > void Fred::discard() throw()
> > {
> >   nextRecycled_ = headRecycled_;
> >   headRecycled_  = this;
> > }

> > bool Fred::flushPool() throw()
> > {
> >   bool stuffGotDeleted = (headRecycled_ != NULL);
> >   while (headRecycled_ != NULL)
> >     delete create();
> >   return stuffGotDeleted;
> > }

> > void flushAllPools() throw(bad_alloc)
> > {
> >   unsigned n = 0;
> >   n += Fred::flushPool();

> >   if (n == 0)
> >     throw bad_alloc();
> > }

> > int main()
> > {
> >   set_new_handler(flushAllPools);
> > }
> > ========================================

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Joseph Thom » Wed, 12 Dec 2001 01:11:25


An apology:   Sorry I followed up my post quoting the entire original
content.  I'm pretty much a a newbie here, and won't do it again.

Dominik, thanks much.  Initialization waws definitely the problem.
Adding that line fixed it.  Why is this considered an "undefined
reference", though?  Just the compiler's choice?

Thanks again,
Joe

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Joseph Thom » Wed, 12 Dec 2001 01:14:50


Just FYI- in this sample program, this line does the fix.

Fred* Fred::headRecycled_ = NULL;

This initialization with the static declaration is not necessary, and
produces a re-declared as error.

-Joe

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by David Harmo » Wed, 12 Dec 2001 02:35:28


On 9 Dec 2001 22:10:52 -0800 in comp.lang.c++,


>When I compile the following program (taken directly from the C++ FAQ
>book), I get undefined reference errors for an element in the
>program's defined class.

This issue is covered in Marshall Cline's C++ FAQ.  See the topic
"[10.10] Why are classes with static data members getting linker errors?".
It is always good to check the FAQ before posting.  You can get the FAQ
at:
  http://www.parashift.com/cpp-faq-lite/    

Quote:>I'd read that uninstalling/reinstalling c++ libs fixes the problem.

Who gave you that bogus advice?
 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Erik Max Franci » Wed, 12 Dec 2001 05:49:21



> An apology:   Sorry I followed up my post quoting the entire original
> content.  I'm pretty much a a newbie here, and won't do it again.

> Dominik, thanks much.  Initialization waws definitely the problem.
> Adding that line fixed it.  Why is this considered an "undefined
> reference", though?  Just the compiler's choice?

This is a Standard C++ issue.  Static members need to be defined in the
source file, or the linker won't be able to find them.  (Note that there
is an exception for static const integral members for which you don't
take the address; in that case most compilers will allow you to
initialize them in the class declaration and no corresponding definition
is required.)

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.

 
 
 

Plz help- "undefined reference" error in short sample c++ program

Post by Erik Max Franci » Wed, 12 Dec 2001 05:50:08



> This initialization with the static declaration is not necessary, and
> produces a re-declared as error.

Yes.  Initialization within the declaration for static members is only
allowed when they're const of integral type, and in that case they
needn't be defined in the relevant source file.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.