Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help with undefined references

Reply
Thread Tools

Help with undefined references

 
 
sieg1974@yahoo.com
Guest
Posts: n/a
 
      08-15-2006
Hi,

I know it must be a simple mistake, but I'm just learning C++ and can't
figure out what's wrong. Any help will be vey much appreciated
I got the following error messages when the following program is
compiled and linked. What could be wrong?

Thanks,

Andre

/tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
/tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
/tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
collect2: ld returned 1 exit status

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();
~Singleton();

private:
Singleton();
static Singleton * theSingleton;
};

Singleton::Singleton()
{
std::cout << "Singleton constructot\n";
}

Singleton::~Singleton()
{
std::cout << "Singleton destructor\n";
}

Singleton * Singleton::Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
}

Singleton * theSingleton = 0;

int main()
{
return( 0 );
}

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      08-15-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> I know it must be a simple mistake, but I'm just learning C++ and can't
> figure out what's wrong. Any help will be vey much appreciated
> I got the following error messages when the following program is
> compiled and linked. What could be wrong?
>
> Thanks,
>
> Andre
>
> /tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
> : undefined reference to `Singleton::theSingleton'
> /tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
> : undefined reference to `Singleton::theSingleton'
> /tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
> : undefined reference to `Singleton::theSingleton'
> collect2: ld returned 1 exit status
>
> #include <iostream>
>
> class Singleton
> {
> public:
> static Singleton * Instance();
> ~Singleton();
>
> private:
> Singleton();
> static Singleton * theSingleton;
> };


>
> Singleton * theSingleton = 0;


Should be:

Singleton * Singleton::theSingleton = 0;

The member name needs to be qualified with its class name:

Greg

 
Reply With Quote
 
 
 
 
Isold.Wang@gmail.com
Guest
Posts: n/a
 
      08-15-2006
You can see this book <<Modern C++ Design>>
Chapter 6. Implementing Singletons

(E-Mail Removed) wrote:
> Hi,
>
> I know it must be a simple mistake, but I'm just learning C++ and can't
> figure out what's wrong. Any help will be vey much appreciated
> I got the following error messages when the following program is
> compiled and linked. What could be wrong?
>
> Thanks,
>
> Andre
>
> /tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
> : undefined reference to `Singleton::theSingleton'
> /tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
> : undefined reference to `Singleton::theSingleton'
> /tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
> : undefined reference to `Singleton::theSingleton'
> collect2: ld returned 1 exit status
>
> #include <iostream>
>
> class Singleton
> {
> public:
> static Singleton * Instance();
> ~Singleton();
>
> private:
> Singleton();
> static Singleton * theSingleton;
> };
>
> Singleton::Singleton()
> {
> std::cout << "Singleton constructot\n";
> }
>
> Singleton::~Singleton()
> {
> std::cout << "Singleton destructor\n";
> }
>
> Singleton * Singleton::Instance()
> {
> if( !theSingleton )
> {
> theSingleton = new Singleton();
> }
>
> return( theSingleton );
> }
>
> Singleton * theSingleton = 0;
>
> int main()
> {
> return( 0 );
> }


 
Reply With Quote
 
sieg1974@yahoo.com
Guest
Posts: n/a
 
      08-15-2006
Thanks for the help, I could make it work now. But I have another
question .
I have the following 2 versions of my program. The first one works
fine, but not the second one. I get the same linking erros as before.
Can anyone point out what could be wrong?

Thanks,

Andre

***********
Version 1
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
}

private:
static Singleton * theSingleton;

};

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
}

***********
Version 2
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();

private:
static Singleton * theSingleton;

};

Singleton * Singleton::Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
}

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
}

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-15-2006
(E-Mail Removed) wrote:
> Thanks for the help, I could make it work now. But I have another
> question .
> I have the following 2 versions of my program. The first one works
> fine, but not the second one. I get the same linking erros as before.


Same, which?

> Can anyone point out what could be wrong?
>
> Thanks,
>
> Andre
>
> ***********
> Version 1
> ***********
>
> #include <iostream>

^^^^^^^^^^^^^^^^^^^
You don't need this since you use nothing from that header.

>
> class Singleton
> {
> public:
> static Singleton * Instance()
> {
> if( !theSingleton )
> {
> theSingleton = new Singleton();
> }
>
> return( theSingleton );


I really wish you'd drop the parentheses around the expression in the
'return' statement. Makes your code look amateurish. 'return' is not
a function. There is no need for parentheses!

> }
>
> private:
> static Singleton * theSingleton;
>
> };
>
> Singleton * Singleton::theSingleton = 0;
>
> int main()
> {
> return( 0 );


Ugh!

> }
>
> ***********
> Version 2
> ***********
>
> #include <iostream>
>
> class Singleton
> {
> public:
> static Singleton * Instance();
>
> private:
> static Singleton * theSingleton;
>
> };
>
> Singleton * Singleton::Instance()
> {
> if( !theSingleton )
> {
> theSingleton = new Singleton();
> }
>
> return( theSingleton );


Yuck!

> }
>
> Singleton * Singleton::theSingleton = 0;
>
> int main()
> {
> return( 0 );


Yechh!

> }


I don't get any link errors if I try building your programs from
the source code you posted.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      08-16-2006

(E-Mail Removed) wrote:
> Thanks for the help, I could make it work now. But I have another
> question .
> I have the following 2 versions of my program. The first one works
> fine, but not the second one. I get the same linking erros as before.
> Can anyone point out what could be wrong?
>
> Thanks,
>
> Andre
>
> ***********
> Version 1
> ***********
>
> #include <iostream>
>
> class Singleton
> {
> public:
> static Singleton * Instance()
> {
> if( !theSingleton )
> {
> theSingleton = new Singleton();


try:
theSingleton = new Singleton;

> }
>
> return( theSingleton );


return theSingleton;

> }
>
> private:
> static Singleton * theSingleton;
>
> };
>
> Singleton * Singleton::theSingleton = 0;
>
> int main()
> {
> return( 0 );
> }
>
> ***********
> Version 2
> ***********
>
> #include <iostream>
>
> class Singleton
> {
> public:
> static Singleton * Instance();
>
> private:
> static Singleton * theSingleton;
>
> };
>
> Singleton * Singleton::Instance()
> {
> if( !theSingleton )
> {
> theSingleton = new Singleton();


same changes...
theSingleton = new Singleton;

> }
>
> return( theSingleton );
> }
>
> Singleton * Singleton::theSingleton = 0;
>
> int main()
> {
> return( 0 );


return 0;

> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help me with undefined references pavan734@gmail.com C++ 5 06-30-2006 06:13 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments