473,394 Members | 1,271 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,394 software developers and data experts.

Putting a singleton in a DLL

2
I'm getting errors about a singleton when I put it into a dll. The dll compiles without any problems. However, when I try to link it to another dll I get the following error.

libFoo-Bar error LNK2001: unresolved external symbol "private: static class MSGTMS:ObjectManager * MSGTMS::ObjectPostManager::m_instance" (?m_instance@ObjectManager@MSGTMS@@0PAV12@A)


The Singleton class looks like this
Expand|Select|Wrap|Line Numbers
  1. #define LIBMSGTM_Managers_EXPORT __declspec (dllexport)
  2.  
  3. namespace MSGTMS
  4. {
  5.  
  6. class LIBMSGTM_Managers_EXPORT ObjectManager
  7. {
  8. public:
  9.     static ObjectManager* instance()
  10.     {
  11.         if (m_instance==0)
  12.             m_instance=new ObjectManager;
  13.         return m_instance;
  14.     }
  15.  
  16.     void destroy()
  17.     {
  18.         if (m_instance!=0)
  19.             delete m_instance;
  20.     }
  21.  
  22.     bool updateFPStatus(const unsigned int FPindex, FencePostStatus& fpStatus);
  23.     bool updateFP(const unsigned int FPindex, FencePost& fp);
  24.  
  25.     bool isFPActive(const unsigned int FP);
  26.     bool getFP(const unsigned int FPindex, FencePost& fp);
  27.     bool getUpdatedStatus(bool* arrayEightFencePosts);
  28.  
  29.     bool getUpdatedPosition(bool* arrayEightFencePosts);
  30.  
  31.     bool deleteFP(const unsigned int FPindex);
  32.  
  33. protected:
  34.     ObjectManager();
  35.     ~ObjectManager();
  36.     Object m_Object[8];
  37.  
  38.  
  39. private:
  40.     static ObjectManager* m_instance;
  41.     CRITICAL_SECTION cs;  //For multithreading protection
  42.  
  43. };
  44. }//namespace MSGTMS
  45.  
  46. Implementation
  47.  
  48. namespace MSGTMS
  49. {
  50. ObjectManager* ObjectManager::m_instance=0;
  51.  
  52. //Constructor Destructor and other things implemented
  53.  
  54. }

Should I be doing something else to a static variable in a dll?
Jan 26 '08 #1
1 4377
weaknessforcats
9,208 Expert Mod 8TB
private:
static ObjectManager* m_instance;
You are not exporting the m_instance.

The error is that m_instance should not be a member of this singleton class.

Instead the m_instance should be in an anonymous namespace accessible onbly by the Instance() function. You export the Instance function().

There is an article in the C++ HowTos forum on singleton classes that shows the correct code setup.

BTW: m_ does not mean a member variable. It just identifies that the name of the variable starts with m_. Avoid naming conventions that lead to assumptions.
Jan 27 '08 #2

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

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.