473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 );
}

Aug 15 '06 #1
5 2290

si******@yahoo.com 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

Aug 15 '06 #2
You can see this book <<Modern C++ Design>>
Chapter 6. Implementing Singletons
:)
si******@yahoo.com 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 );
}
Aug 15 '06 #3
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 );
}

Aug 15 '06 #4
si******@yahoo.com 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
Aug 15 '06 #5

si******@yahoo.com 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;
}
Aug 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Pemburger | last post by:
From: pemburger@aol.com I've tried the W3C MarkUp Validation Service for the following web page: http://www.coverscript.com The report given by W3C shows 300 plus errors? I am not able to...
2
by: Reply Via Newsgroup | last post by:
Folks, Windows, and refering to them (be they in seperate frames or in a popup) always catch me... There is parent, top, self, the name of the window itself and where it sits when refering to...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
2
by: DD | last post by:
I have developed a a nice little software package(Packaged with Wise and SageKey) My product works great in a clean enviroment on Win98 and Win2k and XP As soon as i introduce Office 2000 Pro onto...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
5
by: pavan734 | last post by:
Hi, Iam facing problem with undefined reference linking errors. To explain in more detail.. I have a class called TOP. In its constructor I have instantiated many other class objects using...
2
by: embz | last post by:
this post concerns three pages. 1. this page: http://www.katherine-designs.com/sendemail.php i get the following errors: a lot of it seems to deal with the PHP code i inserted to the page....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.