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: -
#include <iostream>
-
-
using namespace std;
-
-
class ClientManager
-
{
-
public:
-
static ClientManager* Begin()
-
{
-
if(ClientManager::instance == NULL)
-
{
-
cout<<"Creating a new ClientManager.\n";
-
ClientManager::instance = new ClientManager();
-
}
-
else
-
{
-
cout<<"Retrieving the existing ClientManager.\n";
-
}
-
-
return (ClientManager*) ClientManager::instance;
-
}
-
-
~ClientManager()
-
{
-
delete(ClientManager::instance);
-
cout<<"ClientManager destroyed.\n";
-
}
-
-
void PerformBasicFunction()
-
{
-
cout<<"Performing a generic function.\n";
-
}
-
-
private:
-
static void* instance;
-
ClientManager()
-
{
-
cout<<"ClientManager created.\n";
-
}
-
};
-
-
int main()
-
{
-
cout<<"Main loop called.\n";
-
ClientManager* cm = ClientManager::Begin();
-
cout<<"Main loop ended.\n";
-
return 0;
-
}
-
-
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
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: -
#include <iostream>
-
-
using namespace std;
-
-
class ClientManager
-
{
-
public:
-
static ClientManager* Begin()
-
{
-
if(ClientManager::instance == NULL)
-
{
-
cout<<"Creating a new ClientManager.\n";
-
ClientManager::instance = new ClientManager();
-
}
-
else
-
{
-
cout<<"Retrieving the existing ClientManager.\n";
-
}
-
-
return (ClientManager*) ClientManager::instance;
-
}
-
-
~ClientManager()
-
{
-
delete(ClientManager::instance);
-
cout<<"ClientManager destroyed.\n";
-
}
-
-
void PerformBasicFunction()
-
{
-
cout<<"Performing a generic function.\n";
-
}
-
-
private:
-
static void* instance;
-
ClientManager()
-
{
-
cout<<"ClientManager created.\n";
-
}
-
};
-
-
int main()
-
{
-
cout<<"Main loop called.\n";
-
ClientManager* cm = ClientManager::Begin();
-
cout<<"Main loop ended.\n";
-
return 0;
-
}
-
-
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. -
static ClientManager* Begin()
-
{
-
static void * instance;
-
....
-
}
-
Raghuram
JosAH 11,448
Recognized Expert MVP
Did you *define* your static variable 'instance' somewhere in a .cpp file?
kind regards,
Jos
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: -
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
class toasty
-
{
-
public:
-
string s;
-
void shout()
-
{
-
cout<<s<<endl;
-
}
-
};
-
-
int main()
-
{
-
toasty* t = new toasty();
-
t->s = "Hello Bytes.com!";
-
t->shout();
-
}
-
-
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 - void *ClientManager::instance;
with any initialiser should you wish to have one.
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: -
#include <iostream>
-
-
using namespace std;
-
-
class ClientManager
-
{
-
public:
-
static ClientManager* Begin()
-
{
-
if(ClientManager::instance == NULL)
-
{
-
cout<<"Creating a new ClientManager.\n";
-
ClientManager::instance = new ClientManager();
-
}
-
else
-
{
-
cout<<"Retrieving the existing ClientManager.\n";
-
}
-
-
return ClientManager::instance;
-
}
-
-
~ClientManager()
-
{
-
ClientManager::instance = NULL;
-
cout<<"ClientManager destroyed.\n";
-
}
-
-
void PerformBasicFunction()
-
{
-
cout<<"Performing a generic function.\n";
-
}
-
-
private:
-
static ClientManager* instance;
-
ClientManager()
-
{
-
cout<<"ClientManager created.\n";
-
}
-
};
-
-
ClientManager* ClientManager::instance;
-
-
void singletonTest()
-
{
-
cout<<"Retrieving a ClientManager instance.\n";
-
ClientManager* cm = ClientManager::Begin();
-
cm->PerformBasicFunction();
-
}
-
-
int main()
-
{
-
cout<<"Main loop called.\n";
-
singletonTest();
-
singletonTest();
-
cout<<"Main loop ended.\n";
-
return 0;
-
}
-
-
Thanks again for all of your help,
Motoma
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: -
#include <iostream>
-
-
using namespace std;
-
-
class ClientManager
-
{
-
public:
-
static ClientManager* Begin()
-
{
-
if(ClientManager::instance == NULL)
-
{
-
cout<<"Creating a new ClientManager.\n";
-
ClientManager::instance = new ClientManager();
-
}
-
else
-
{
-
cout<<"Retrieving the existing ClientManager.\n";
-
}
-
-
return ClientManager::instance;
-
}
-
-
~ClientManager()
-
{
-
ClientManager::instance = NULL;
-
cout<<"ClientManager destroyed.\n";
-
}
-
-
void PerformBasicFunction()
-
{
-
cout<<"Performing a generic function.\n";
-
}
-
-
private:
-
static ClientManager* instance;
-
ClientManager()
-
{
-
cout<<"ClientManager created.\n";
-
}
-
};
-
-
ClientManager* ClientManager::instance;
-
-
void singletonTest()
-
{
-
cout<<"Retrieving a ClientManager instance.\n";
-
ClientManager* cm = ClientManager::Begin();
-
cm->PerformBasicFunction();
-
}
-
-
int main()
-
{
-
cout<<"Main loop called.\n";
-
singletonTest();
-
singletonTest();
-
cout<<"Main loop ended.\n";
-
return 0;
-
}
-
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
...
|
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;
| |
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);...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| | |