Thread View: alt.comp.lang.learn.c-c++
3 messages
3 total messages
Started by Max Coppin
Sun, 20 Jan 2002 23:19
Singleton pattern
Author: Max Coppin
Date: Sun, 20 Jan 2002 23:19
Date: Sun, 20 Jan 2002 23:19
52 lines
2874 bytes
2874 bytes
Hi,
I'm trying to convert one of my C++ programs to use the singleton pattern.
I've got the Design Patterns book by Gamma et all and am just trying to copy
a piece of code from that. The code is below, as is the error i get. My
first question is where should these pieces of code go? I presume that the
definition goes in a .h and the implementation in a .cpp and then call in
from main.cpp or whatever. Is this correct, can anyone help?
maxcoppin@btinternet.com
class MazeFactory
{
public:
static MazeFactory* Instance();
protected:
MazeFactory();
private:
static MazeFactory* _instance;
};
MazeFactory* MazeFactory::_instance=0;
MazeFactory* MazeFactory::Instance()
{
if(_instance == 0)
{
_instance= new MazeFactory;
}
return _instance;
}
ERROR MESSAGE :-
--------------------Configuration: ProjTemplate - Win32
Release--------------------
Compiling...
main.cpp
test.cpp
Generating Code...
Linking...
test.obj : error LNK2001: unresolved external symbol "protected: __thiscall
MazeFactory::MazeFactory(void)" (??0MazeFactory@@IAE@XZ)
Release/ProjTemplate.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
ProjTemplate.exe - 2 error(s), 0 warning(s)
Re: Singleton pattern
Author: heejune kim
Date: Mon, 21 Jan 2002 02:35
Date: Mon, 21 Jan 2002 02:35
32 lines
2414 bytes
2414 bytes
> class MazeFactory
> {
> public:
> static MazeFactory* Instance();
> protected:
> MazeFactory(); // change this line to 'MazeFactory() {;}'
>
> private:
> static MazeFactory* _instance;
> };
>
> ERROR MESSAGE :-
>
> --------------------Configuration: ProjTemplate - Win32
> Release--------------------
> Compiling...
> main.cpp
> test.cpp
> Generating Code...
> Linking...
> test.obj : error LNK2001: unresolved external symbol "protected:
__thiscall
> MazeFactory::MazeFactory(void)" (??0MazeFactory@@IAE@XZ)
> Release/ProjTemplate.exe : fatal error LNK1120: 1 unresolved externals
> Error executing link.exe.
>
> ProjTemplate.exe - 2 error(s), 0 warning(s)
in header, constructor defined, but its implementation doens't exist. so
unresolved symbol error occuered. So, that code has to be edited blank
constructor implementation provided.
Re: Singleton pattern
Author: Robert C. Martin
Date: Wed, 23 Jan 2002 18:27
Date: Wed, 23 Jan 2002 18:27
71 lines
3410 bytes
3410 bytes
On Sun, 20 Jan 2002 23:19:36 -0000, "Max Coppin"
<maxcoppin@btinternet.com> wrote:
>Hi,
>I'm trying to convert one of my C++ programs to use the singleton pattern.
>I've got the Design Patterns book by Gamma et all and am just trying to copy
>a piece of code from that. The code is below, as is the error i get. My
>first question is where should these pieces of code go? I presume that the
>definition goes in a .h and the implementation in a .cpp and then call in
>from main.cpp or whatever. Is this correct, can anyone help?
>
>maxcoppin@btinternet.com
>
Try adding the line below:
>
>class MazeFactory
>{
>public:
> static MazeFactory* Instance();
>protected:
> MazeFactory();
>
>private:
> static MazeFactory* _instance;
>};
>
//Add this line
MazeFactory::MazeFactory() {}
>
>MazeFactory* MazeFactory::_instance=0;
>
>MazeFactory* MazeFactory::Instance()
>{
> if(_instance == 0)
> {
> _instance= new MazeFactory;
> }
> return _instance;
>}
>
>
>ERROR MESSAGE :-
>
>--------------------Configuration: ProjTemplate - Win32
>Release--------------------
>Compiling...
>main.cpp
>test.cpp
>Generating Code...
>Linking...
>test.obj : error LNK2001: unresolved external symbol "protected: __thiscall
>MazeFactory::MazeFactory(void)" (??0MazeFactory@@IAE@XZ)
>Release/ProjTemplate.exe : fatal error LNK1120: 1 unresolved externals
>Error executing link.exe.
>
>ProjTemplate.exe - 2 error(s), 0 warning(s)
>
>
Robert C. Martin | "Uncle Bob" | Software Consultants
Object Mentor Inc. | rmartin@objectmentor.com | We'll help you get
PO Box 5757 | Tel: (800) 338-6716 | your projects done.
565 Lakeview Pkwy | Fax: (847) 573-1658 | www.objectmentor.com
Suite 135 | | www.XProgramming.com
Vernon Hills, IL, | Training and Mentoring | www.junit.org
60061 | OO, XP, Java, C++, Python|
"One of the great commandments of science is:
'Mistrust arguments from authority.'" -- Carl Sagan
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads