473,583 Members | 2,875 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 ._ZN13ClientMan ager5BeginEv[ClientManager:: Begin()]+0x8): undefined reference to `ClientManager: :instance'
main.cpp:(.text ._ZN13ClientMan ager5BeginEv[ClientManager:: Begin()]+0x42): undefined reference to `ClientManager: :instance'
main.cpp:(.text ._ZN13ClientMan ager5BeginEv[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 6279
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
3072
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 across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null...
49
14461
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
6
6231
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 downloaded the sources of id3lib. I've included the headers (there are no other files) in my project in Qt designer, then created an object from my...
8
9763
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 classname::classname(){
3
6824
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
6479
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); td.onclick = myfunction; function myfunction(event) { alert(event);
1
4391
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 System.Diagnostics; public delegate bool IECallBack(int hwnd, int lParam);
3
4282
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 commented out code on an event stub. The After Update stub was written but the one line piece of code was commented out.
5
1487
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 But now I have some new code I deployed and I'm getting a null reference error when looping through a datatable (for each r in dt.rows...). I'm...
0
7888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8185
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5689
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.