473,883 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

singleton problem

Hello!

I have a class called Outdoors which is a singleton see below for definition
and a main that call this instance method.

Now to my problem this piece of code doesn't compile I get the following
error I can't understand what it is.
start.obj : error LNK2001: unresolved external symbol "private: __thiscall
Outdoors::Outdo ors(void)" (??0Outdoors@@A AE@XZ)
Debug/slask.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
#include "outdoors.h "
int main()
{
Outdoors* base = Outdoors::insta nce();
return 0;
}

//outdoors.h
//********
class Outdoors
{
public:
static Outdoors* instance()
{
if (onlyInstance == 0)
onlyInstance = new Outdoors();

return onlyInstance;
}

private:
Outdoors();
static Outdoors* onlyInstance;
};
Outdoors* Outdoors::onlyI nstance = 0;

Many Thanks!
//Tony
Jul 23 '05 #1
6 3034
Tony Johansson wrote:
Hello!

I have a class called Outdoors which is a singleton see below for definition
and a main that call this instance method.

Now to my problem this piece of code doesn't compile I get the following
error I can't understand what it is.
start.obj : error LNK2001: unresolved external symbol "private: __thiscall
Outdoors::Outdo ors(void)" (??0Outdoors@@A AE@XZ)
Debug/slask.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
#include "outdoors.h "
int main()
{
Outdoors* base = Outdoors::insta nce();
return 0;
}

//outdoors.h
//********
class Outdoors
{
public:
static Outdoors* instance()
{
if (onlyInstance == 0)
onlyInstance = new Outdoors();

return onlyInstance;
}

private:
Outdoors();
Try replacing above line with: Outdoors() {};

(You declare the constructor but never define it.)

-Mark
static Outdoors* onlyInstance;
};
Outdoors* Outdoors::onlyI nstance = 0;

Many Thanks!
//Tony

Jul 23 '05 #2
> Now to my problem this piece of code doesn't compile I get the following
error I can't understand what it is.
start.obj : error LNK2001: unresolved external symbol "private: __thiscall
Outdoors::Outdo ors(void)" (??0Outdoors@@A AE@XZ)
Debug/slask.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


You have declared the default constructor for Outdoors (as private), and you
are using it (when creating the instance), but you have not defined it.
Define it (at least as empty, unless you have any logic to put in it), and
the error will disappear.

BTW there are other errors:

1) You have *defined* Outdoors::onlyI nstance in the header. This would mean
that if the header is included in two modules, there will be multiple
definition of the same object, which is forbidden (and will most probably
cause the linker to complain). Please create a new module (e.g.
outdoors.cpp) and define Outdoors::onlyI nstance there.

2) Your code never destroys the singleton. That may be what is desired, but
be aware of that fact. You will have at least a memory leak for the
singleton object, if not other leaks. As an example, if the singleton keeps
a file open and writes to it, and if it closes the file in its destructor,
the file may be closed by the OS instead of your application, which means
that some data may be buffered and not written at the end, i.e. the file may
get corrupted. One can easily think of more examples whenever the Outdoors
destructor is nontrivial.

3) It is too easy to delete the singleton (e.g. delete
Outdoors::insta nce()).

To solve problems 2 and 3, usually the following pattern is used:

// outdoors.h
class Outdoors
{
public:

static Outdoors &Outdoors::inst ance();
// etc.
private:
Outdoors() { /* ... */ } // note that this is a definition as well
// etc.
};

// outdoors.cpp
Outdoors &Outdoors::inst ance()
{
static Outdoors onlyInstance;
return onlyInstance;
}

Note that instance() returns a reference instead of pointer, so it would be
harder to delete it inadvertently. C++ Standard guarantees that Outdoors
constructor will be called first time the instance() method is called, and
destructor will be called upon returning from main(). However, there is also
a couple of pitfalls with this approach: (1) it is unspecified when exactly
after main() the singleton will be destroyed; particularly this is important
if you have multiple singletons depending on each other, (2) possible
threading issues which are OT here.

Rade
Jul 23 '05 #3
Tony Johansson wrote:
Hello!

I have a class called Outdoors which is a singleton see below for definition
and a main that call this instance method.

Now to my problem this piece of code doesn't compile I get the following
error I can't understand what it is.
start.obj : error LNK2001: unresolved external symbol "private: __thiscall
Outdoors::Outdo ors(void)" (??0Outdoors@@A AE@XZ)
Debug/slask.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
#include "outdoors.h "
int main()
{
Outdoors* base = Outdoors::insta nce();
return 0;
}

//outdoors.h
//********
class Outdoors
{
public:
static Outdoors* instance()
{
if (onlyInstance == 0)
onlyInstance = new Outdoors();

return onlyInstance;
}

private:
Outdoors();
static Outdoors* onlyInstance;
};
Outdoors* Outdoors::onlyI nstance = 0;

Many Thanks!
//Tony

When you declare a static member variable in a class you need to
define the variable somewhere to avoid a linker error.

Put the following at the top of your .cpp file and the link error
will go away.

Outdoors* Outdoors::onlyI nstance = NULL;
Jul 23 '05 #4
Mark P wrote:

Try replacing above line with: Outdoors() {};

(You declare the constructor but never define it.)

-Mark
static Outdoors* onlyInstance;
};
Outdoors* Outdoors::onlyI nstance = 0;

Many Thanks!
//Tony


It's a SINGLETON he does not want anyone to be able to new an instance
directly.

That's why the cstor is private.
Jul 23 '05 #5

"Prog37" <pr****@comcast .net> wrote in message
news:0K******** ************@co mcast.com...
Tony Johansson wrote:
Hello!

I have a class called Outdoors which is a singleton see below for
definition and a main that call this instance method.

Now to my problem this piece of code doesn't compile I get the following
error I can't understand what it is.
start.obj : error LNK2001: unresolved external symbol "private:
__thiscall Outdoors::Outdo ors(void)" (??0Outdoors@@A AE@XZ)
Debug/slask.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
#include "outdoors.h "
int main()
{
Outdoors* base = Outdoors::insta nce();
return 0;
}

//outdoors.h
//********
class Outdoors
{
public:
static Outdoors* instance()
{
if (onlyInstance == 0)
onlyInstance = new Outdoors();

return onlyInstance;
}

private:
Outdoors();
static Outdoors* onlyInstance;
};
Outdoors* Outdoors::onlyI nstance = 0;

Many Thanks!
//Tony

When you declare a static member variable in a class you need to
define the variable somewhere to avoid a linker error.

Put the following at the top of your .cpp file and the link error
will go away.

Outdoors* Outdoors::onlyI nstance = NULL;


He's already got that, but in the wrong place. It shouldn't be in a header
file, but in an implementation file, such as Outdoors.cpp.

But the link error he's getting is related to the constructor, which is not
defined. One solution is to add an empty implementation of the constructor
in the code above. But Rade suggests a better alternative, I think.

-Howard
Jul 23 '05 #6
Prog37 wrote:
Mark P wrote:

Try replacing above line with: Outdoors() {};

(You declare the constructor but never define it.)

-Mark
static Outdoors* onlyInstance;
};
Outdoors* Outdoors::onlyI nstance = 0;

Many Thanks!
//Tony


It's a SINGLETON he does not want anyone to be able to new an instance
directly.

That's why the cstor is private.


What has that got to do with my advice? If you use new, you better have
a constructor, and if you also declare a constructor, you better define it.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
6743
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that only the first is safe for multi-threaded appliactions. I want to know why this so. The class is as follows:
1
2457
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 template <class T> class Singleton : public T { public: static T * Instance();
3
3933
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
7
3622
by: Stephen Brown | last post by:
I have some strange behavior on my web server that seems to point to garbage collection. I have a singleton that tracks web activity on my web site. The singleton works great, except that it restarts periodically. The web services have not been restarted and the error log shows no problems. It is the same problem on 2 different servers, but is worse on the most used server. On the most used server, it gets restarted 5 to 10 times a day...
21
2474
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
3
2973
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise Singleton.__single
6
3203
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the singleton is initialized as a static variable , it seems there is some threading issue . Is it the issue during singleton initialization only , or during the access also? If the singleton is per thread basis (then no more singleton though ), and...
3
435
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: Singleton * instance() { if(pSingleton==NULL) pSingleton = new Singleton; return pSingleton; }
2
1593
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates" I mean classes for which terrible problems would clearly arise if more than one instance were to exist. But as I'm getting into the design of this new solution, I'm realizing that a large percentage of the classes _could be_ implemented as...
4
2836
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
0
9938
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9791
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10847
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9572
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7971
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4611
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
2
4218
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3232
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.