 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Aug 15, 2006 9:10 am Post subject: Help with undefined references |
|
|
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 );
} |
|
| Back to top |
|
 |
Isold.Wang@gmail.com Guest
|
Posted: Tue Aug 15, 2006 9:10 am Post subject: Re: Help with undefined references |
|
|
You can see this book <<Modern C++ Design>>
Chapter 6. Implementing Singletons
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
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
|
Posted: Tue Aug 15, 2006 9:10 am Post subject: Re: Help with undefined references |
|
|
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
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
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: Help with undefined references |
|
|
sieg1974 (AT) yahoo (DOT) com wrote:
| Quote: | 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;
| 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 |
|
 |
|
|
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
|
|