C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with undefined references

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Aug 15, 2006 9:10 am    Post subject: Help with undefined references Reply with quote



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 Smile
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 );
}
Back to top
Isold.Wang@gmail.com
Guest





PostPosted: Tue Aug 15, 2006 9:10 am    Post subject: Re: Help with undefined references Reply with quote



You can see this book <<Modern C++ Design>>
Chapter 6. Implementing Singletons
Smile
sieg1974 (AT) yahoo (DOT) com wrote:
Quote:
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 Smile
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 );
}
Back to top
Greg
Guest





PostPosted: Tue Aug 15, 2006 9:10 am    Post subject: Re: Help with undefined references Reply with quote



sieg1974 (AT) yahoo (DOT) com wrote:
Quote:
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 Smile
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
Back to top
Salt_Peter
Guest





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: Re: Help with undefined references Reply with quote

sieg1974 (AT) yahoo (DOT) com wrote:
Quote:
Thanks for the help, I could make it work now. But I have another
question Smile.
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;

Quote:
}

return( theSingleton );

return theSingleton;

Quote:
}

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;

Quote:
}

return( theSingleton );
}

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );

return 0;

> }
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.