473,512 Members | 15,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton Attempt, Static Variable, "Undefined reference" error.

Motoma
3,237 Recognized Expert Specialist
Good evening everyone.
I am starting to re-explore C++, and I wanted to build a singleton class. Unfortunately, when I set things up as I do in PHP, it doesn't work out for me. I hope that the problem is something small and obvious, but here is what I have:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class ClientManager
  6. {
  7. public:
  8.   static ClientManager* Begin()
  9.   {
  10.     if(ClientManager::instance == NULL)
  11.       {
  12.         cout<<"Creating a new ClientManager.\n";
  13.         ClientManager::instance = new ClientManager();
  14.       }
  15.     else
  16.       {
  17.         cout<<"Retrieving the existing ClientManager.\n";
  18.       }
  19.  
  20.     return (ClientManager*) ClientManager::instance;
  21.   }
  22.  
  23.   ~ClientManager()
  24.   {
  25.     delete(ClientManager::instance);
  26.     cout<<"ClientManager destroyed.\n";
  27.   }
  28.  
  29.   void PerformBasicFunction()
  30.   {
  31.     cout<<"Performing a generic function.\n";
  32.   }
  33.  
  34. private:
  35.   static void* instance;
  36.   ClientManager()
  37.   {
  38.     cout<<"ClientManager created.\n";
  39.   }
  40. };
  41.  
  42. int main()
  43. {
  44.   cout<<"Main loop called.\n";
  45.   ClientManager* cm = ClientManager::Begin();
  46.   cout<<"Main loop ended.\n";
  47.   return 0;
  48. }
  49.  
  50.  
When I attempt to compile this I receive the following errors:

main.cpp:(.text._ZN13ClientManager5BeginEv[ClientManager::Begin()]+0x8): undefined reference to `ClientManager::instance'
main.cpp:(.text._ZN13ClientManager5BeginEv[ClientManager::Begin()]+0x42): undefined reference to `ClientManager::instance'
main.cpp:(.text._ZN13ClientManager5BeginEv[ClientManager::Begin()]+0x7c): undefined reference to `ClientManager::instance'
I have looked at everything that I could think of, and everything seems to the in place. I am sure that it is my inexperience that is preventing me from seeing the issue, so if anyone has an idea, I would be very appreciative.

Thanks,
Motoma
Apr 28 '08 #1
8 6273
gpraghuram
1,275 Recognized Expert Top Contributor
Good evening everyone.
I am starting to re-explore C++, and I wanted to build a singleton class. Unfortunately, when I set things up as I do in PHP, it doesn't work out for me. I hope that the problem is something small and obvious, but here is what I have:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class ClientManager
  6. {
  7. public:
  8.   static ClientManager* Begin()
  9.   {
  10.     if(ClientManager::instance == NULL)
  11.       {
  12.         cout<<"Creating a new ClientManager.\n";
  13.         ClientManager::instance = new ClientManager();
  14.       }
  15.     else
  16.       {
  17.         cout<<"Retrieving the existing ClientManager.\n";
  18.       }
  19.  
  20.     return (ClientManager*) ClientManager::instance;
  21.   }
  22.  
  23.   ~ClientManager()
  24.   {
  25.     delete(ClientManager::instance);
  26.     cout<<"ClientManager destroyed.\n";
  27.   }
  28.  
  29.   void PerformBasicFunction()
  30.   {
  31.     cout<<"Performing a generic function.\n";
  32.   }
  33.  
  34. private:
  35.   static void* instance;
  36.   ClientManager()
  37.   {
  38.     cout<<"ClientManager created.\n";
  39.   }
  40. };
  41.  
  42. int main()
  43. {
  44.   cout<<"Main loop called.\n";
  45.   ClientManager* cm = ClientManager::Begin();
  46.   cout<<"Main loop ended.\n";
  47.   return 0;
  48. }
  49.  
  50.  
When I attempt to compile this I receive the following errors:



I have looked at everything that I could think of, and everything seems to the in place. I am sure that it is my inexperience that is preventing me from seeing the issue, so if anyone has an idea, I would be very appreciative.

Thanks,
Motoma

When the instance variable is defined inside the Begin method then the error will be gone
I tried implementing a singleton class using the approcah which i explained you.

Expand|Select|Wrap|Line Numbers
  1. static ClientManager* Begin()
  2.   {
  3.     static void * instance;
  4. ....
  5. }
  6.  
Raghuram
Apr 28 '08 #2
JosAH
11,448 Recognized Expert MVP
Did you *define* your static variable 'instance' somewhere in a .cpp file?

kind regards,

Jos
Apr 28 '08 #3
Motoma
3,237 Recognized Expert Specialist
Did you *define* your static variable 'instance' somewhere in a .cpp file?

kind regards,

Jos
Does line 35 not count?

In other circumstances, "defining" variables this way seems to work correctly:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class toasty
  7. {
  8. public:
  9.   string s;
  10.   void shout()
  11.   {
  12.     cout<<s<<endl;
  13.   }
  14. };
  15.  
  16. int main()
  17. {
  18.   toasty* t = new toasty();
  19.   t->s = "Hello Bytes.com!";
  20.   t->shout();
  21. }
  22.  
  23.  
Apr 28 '08 #4
Banfa
9,065 Recognized Expert Moderator Expert
Does line 35 not count?

In other circumstances, "defining" variables this way seems to work correctly:
No that is a declaration, that says it exists without actually creating it. The reason that static class variables and normal class variables are different is that when you declare a normal class variable it gets created when the instance of the class is created, but for static class variables where all instances share the same variable then the variable needs to be defined(created) outside of the creation of any instance of the class.

You need this in a cpp file somewhere

Expand|Select|Wrap|Line Numbers
  1. void *ClientManager::instance;
with any initialiser should you wish to have one.
Apr 28 '08 #5
Motoma
3,237 Recognized Expert Specialist
Thanks for that last hint Banfa.

JosAH, my apologies for being too base to see what you meant. I'll chalk this one up to lack of experience.

gpraghuram, thanks for the assistance as well. While your solution does work, it's not exactly what I am looking for, as I would like the instance variable to be accessible inside the class.

For anyone who happens to clamber by, here is my final code listing:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class ClientManager
  6. {
  7. public:
  8.   static ClientManager* Begin()
  9.   {
  10.     if(ClientManager::instance == NULL)
  11.       {
  12.         cout<<"Creating a new ClientManager.\n";
  13.         ClientManager::instance = new ClientManager();
  14.       }
  15.     else
  16.       {
  17.         cout<<"Retrieving the existing ClientManager.\n";
  18.       }
  19.  
  20.     return ClientManager::instance;
  21.   }
  22.  
  23.   ~ClientManager()
  24.   {
  25.     ClientManager::instance = NULL;
  26.     cout<<"ClientManager destroyed.\n";
  27.   }
  28.  
  29.   void PerformBasicFunction()
  30.   {
  31.     cout<<"Performing a generic function.\n";
  32.   }
  33.  
  34. private:
  35.   static ClientManager* instance;
  36.   ClientManager()
  37.   {
  38.     cout<<"ClientManager created.\n";
  39.   }
  40. };
  41.  
  42. ClientManager* ClientManager::instance;
  43.  
  44. void singletonTest()
  45. {
  46.   cout<<"Retrieving a ClientManager instance.\n";
  47.   ClientManager* cm = ClientManager::Begin();
  48.   cm->PerformBasicFunction();
  49. }
  50.  
  51. int main()
  52. {
  53.   cout<<"Main loop called.\n";
  54.   singletonTest();
  55.   singletonTest();
  56.   cout<<"Main loop ended.\n";
  57.   return 0;
  58. }
  59.  
  60.  
Thanks again for all of your help,
Motoma
Apr 28 '08 #6
JosAH
11,448 Recognized Expert MVP
Thanks for that last hint Banfa.

JosAH, my apologies for being too base to see what you meant. I'll chalk this one up to lack of experience.

gpraghuram, thanks for the assistance as well. While your solution does work, it's not exactly what I am looking for, as I would like the instance variable to be accessible inside the class.

For anyone who happens to clamber by, here is my final code listing:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class ClientManager
  6. {
  7. public:
  8.   static ClientManager* Begin()
  9.   {
  10.     if(ClientManager::instance == NULL)
  11.       {
  12.         cout<<"Creating a new ClientManager.\n";
  13.         ClientManager::instance = new ClientManager();
  14.       }
  15.     else
  16.       {
  17.         cout<<"Retrieving the existing ClientManager.\n";
  18.       }
  19.  
  20.     return ClientManager::instance;
  21.   }
  22.  
  23.   ~ClientManager()
  24.   {
  25.     ClientManager::instance = NULL;
  26.     cout<<"ClientManager destroyed.\n";
  27.   }
  28.  
  29.   void PerformBasicFunction()
  30.   {
  31.     cout<<"Performing a generic function.\n";
  32.   }
  33.  
  34. private:
  35.   static ClientManager* instance;
  36.   ClientManager()
  37.   {
  38.     cout<<"ClientManager created.\n";
  39.   }
  40. };
  41.  
  42. ClientManager* ClientManager::instance;
  43.  
  44. void singletonTest()
  45. {
  46.   cout<<"Retrieving a ClientManager instance.\n";
  47.   ClientManager* cm = ClientManager::Begin();
  48.   cm->PerformBasicFunction();
  49. }
  50.  
  51. int main()
  52. {
  53.   cout<<"Main loop called.\n";
  54.   singletonTest();
  55.   singletonTest();
  56.   cout<<"Main loop ended.\n";
  57.   return 0;
  58. }
  59.  
  60.  
Thanks again for all of your help,
Motoma
... and now all your functions are 'inline' functions; don't do it this way; *define*
your class in an .hpp file; that way you *declare* all your functions and static
members. Elsewhere in a .cpp file *define* all those functions and static members.

Or do you really want to *declare* (and *define*) those functions as inline functions?
If you like 'code bloat' please do so ;-)

kind regards,

Jos
Apr 28 '08 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
And, of course, there is this article http://bytes.com/forum/thread656124.html.
Apr 28 '08 #8
Motoma
3,237 Recognized Expert Specialist
... and now all your functions are 'inline' functions; don't do it this way; *define*
your class in an .hpp file; that way you *declare* all your functions and static
members. Elsewhere in a .cpp file *define* all those functions and static members.

Or do you really want to *declare* (and *define*) those functions as inline functions?
If you like 'code bloat' please do so ;-)

kind regards,

Jos
I am aware JosAH, when I was designing my simple test case, I felt that this was more readable than three separate sections of code...the whole trying to help the people who are trying to help me thing.

Perhaps I was wrong.
Apr 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
3063
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
49
14431
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...
6
6217
by: mihailsmilev | last post by:
Hello, let me first describe the situation: I am developing an application using Qt Designer 3.3.5 on OpenSuSE Linux for my mp3 player. So I need to get the id3 tags from the mp3 files, and I've...
8
9731
by: utab | last post by:
Dear all, I am getting an undefined reference error wrn I try to initilialize a static map in the class constructor. my map: map<string,vector<string> > m; and in the constructor ...
3
6820
by: shailendrapalsingh | last post by:
hi all i am trying to read excel file through my program. but when i compile i get "Application is ambiguous reference'. error. i am including using Excel;
5
6465
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
1
4381
by: prithvis.mohanty | last post by:
using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Text; using System.Collections; using...
3
4275
by: christianlott1 | last post by:
I wouldn't have brought this up except that a friend had a similar problem as well. Situation: Code works fine for months, then suddenly breaks. In my case the code broke on a piece of...
5
1479
by: MattB | last post by:
I'm relatively familiar with asp.net and vb.net and have been working in the environment for several years. I've dealt with null reference errors before and generally know how to code around them...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.