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 ._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
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: -
#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 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...
|
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.
|
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...
|
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(){
|
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);
td.onclick = myfunction;
function myfunction(event) {
alert(event);
|
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);
|
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.
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |