473,768 Members | 7,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Build errors on "trivial" proof of concept design pattern

Hi,

I've written a very simple Simpleton design pattern class template. This
was supposed to illustrate a point. Although it compiles without nay
errors, I get link errors (unresolved external), during linking - before
anyone jumps to conclusions that this is OT, please check the linker
error message below, it is NOT OT.

Here are my class declarations/definitions:

template <class T>
class Singleton {

public:
static T& instance();
protected:
Singleton() {}

private:
static T* instance_ ;
Singleton(const Singleton&) {}
void operator=(const Singleton&) {}
};
template <class T>
T& Singleton<T>::i nstance() {
if (!instance_)
instance_ = new T ;

return *instance_;
}
//Simple Test harness
class Test : public Singleton<Test> {
friend class Singleton<Test> ;

public:
int foo(void){ return 1 ; }
void bar(void) { ; }
virtual ~Test() { cout << "Dtor called" << endl ; }

private:
Test() { cout << "Ctor called" << endl ; }
};

The code to test this is below (header stuff not included for brevity)

int main() {
Foo &f = Foo::instance() ;

}
Here is the linker eror I get:

singleton error LNK2001: unresolved external symbol "private: static
class Test * Singleton<class Test>::instance _"

Can anyone spot where I'm going wrong?

Aug 31 '05 #1
5 1477
> Hi,

I've written a very simple Simpleton design pattern class template. This
was supposed to illustrate a point. Although it compiles without nay
errors, I get link errors (unresolved external), during linking - before
anyone jumps to conclusions that this is OT, please check the linker
error message below, it is NOT OT.

Here are my class declarations/definitions:

template <class T>
class Singleton {

public:
static T& instance();
protected:
Singleton() {}

private:
static T* instance_ ;
Where is this static member instanciated?
Singleton(const Singleton&) {}
void operator=(const Singleton&) {}
};
template <class T>
T& Singleton<T>::i nstance() {
if (!instance_)
instance_ = new T ;

return *instance_;
}
//Simple Test harness
class Test : public Singleton<Test> {
friend class Singleton<Test> ;

public:
int foo(void){ return 1 ; }
void bar(void) { ; }
virtual ~Test() { cout << "Dtor called" << endl ; }

private:
Test() { cout << "Ctor called" << endl ; }
};

The code to test this is below (header stuff not included for brevity)

int main() {
Foo &f = Foo::instance() ;

}
Here is the linker eror I get:

singleton error LNK2001: unresolved external symbol "private: static
class Test * Singleton<class Test>::instance _"

Can anyone spot where I'm going wrong?


I don't see where Singleton<Test> ::instance_ is being instanciated.

-Brian

Aug 31 '05 #2


BigBrian wrote:
Hi,

I've written a very simple Simpleton design pattern class template. This
was supposed to illustrate a point. Although it compiles without nay
errors, I get link errors (unresolved external), during linking - before
anyone jumps to conclusions that this is OT, please check the linker
error message below, it is NOT OT.

Here are my class declarations/definitions:

template <class T>
class Singleton {

public:
static T& instance();
protected:
Singleton() {}

private:
static T* instance_ ;

Where is this static member instanciated?

Singleton(const Singleton&) {}
void operator=(const Singleton&) {}
};
template <class T>
T& Singleton<T>::i nstance() {
if (!instance_)
instance_ = new T ;

return *instance_;
}
//Simple Test harness
class Test : public Singleton<Test> {
friend class Singleton<Test> ;

public:
int foo(void){ return 1 ; }
void bar(void) { ; }
virtual ~Test() { cout << "Dtor called" << endl ; }

private:
Test() { cout << "Ctor called" << endl ; }
};

The code to test this is below (header stuff not included for brevity)

int main() {
Foo &f = Foo::instance() ;

}
Here is the linker eror I get:

singleton error LNK2001: unresolved external symbol "private: static
class Test * Singleton<class Test>::instance _"

Can anyone spot where I'm going wrong?

I don't see where Singleton<Test> ::instance_ is being instanciated.

-Brian

Its been done in the instance method (T& Singleton<T>::i nstance())

Aug 31 '05 #3
Alfonso Morra wrote:

Its been done in the instance method (T& Singleton<T>::i nstance())


No.
What BigBrian ment is:
You *declared* a variable instance_, here:
template <class T>
class Singleton {

public:
static T& instance();
protected:
Singleton() {}

private:
static T* instance_ ;


This says: somewhere there is a variable called instance_. It is
a member of template <class T> Singleton.
But you never told the compiler to actually *create* that variable.
It is like: you have a function prototype, which tells everybody:
Somwhere there is a function called .... But then you actually
need to implement it!

eg.

class X
{
// This *declares* the member variable
static int x;
};

int X::x = 5; // and this *defines* (implements) the variable.

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 31 '05 #4
> BigBrian wrote:
Hi,

I've written a very simple Simpleton design pattern class template. This
was supposed to illustrate a point. Although it compiles without nay
errors, I get link errors (unresolved external), during linking - before
anyone jumps to conclusions that this is OT, please check the linker
error message below, it is NOT OT.

Here are my class declarations/definitions:

template <class T>
class Singleton {

public:
static T& instance();
protected:
Singleton() {}

private:
static T* instance_ ;

Where is this static member instanciated?

Singleton(const Singleton&) {}
void operator=(const Singleton&) {}
};
template <class T>
T& Singleton<T>::i nstance() {
if (!instance_)
instance_ = new T ;

return *instance_;
}
//Simple Test harness
class Test : public Singleton<Test> {
friend class Singleton<Test> ;

public:
int foo(void){ return 1 ; }
void bar(void) { ; }
virtual ~Test() { cout << "Dtor called" << endl ; }

private:
Test() { cout << "Ctor called" << endl ; }
};

The code to test this is below (header stuff not included for brevity)

int main() {
Foo &f = Foo::instance() ;

}
Here is the linker eror I get:

singleton error LNK2001: unresolved external symbol "private: static
class Test * Singleton<class Test>::instance _"

Can anyone spot where I'm going wrong?

I don't see where Singleton<Test> ::instance_ is being instanciated.

-Brian

Its been done in the instance method (T& Singleton<T>::i nstance())


But Singleton<Test> ::instance_ is static, so it needs to be
instanciated at file scope.

-Brian

Aug 31 '05 #5


Alfonso Morra wrote:


BigBrian wrote:
Hi,

I've written a very simple Simpleton design pattern class template. This
was supposed to illustrate a point. Although it compiles without nay
errors, I get link errors (unresolved external), during linking - before
anyone jumps to conclusions that this is OT, please check the linker
error message below, it is NOT OT.

Here are my class declarations/definitions:

template <class T>
class Singleton {

public:
static T& instance();
protected:
Singleton() {}

private:
static T* instance_ ;


Where is this static member instanciated?

Singleton(const Singleton&) {}
void operator=(const Singleton&) {}
};
template <class T>
T& Singleton<T>::i nstance() {
if (!instance_)
instance_ = new T ;

return *instance_;
}
//Simple Test harness
class Test : public Singleton<Test> {
friend class Singleton<Test> ;

public:
int foo(void){ return 1 ; }
void bar(void) { ; }
virtual ~Test() { cout << "Dtor called" << endl ; }

private:
Test() { cout << "Ctor called" << endl ; }
};

The code to test this is below (header stuff not included for brevity)

int main() {
Foo &f = Foo::instance() ;

}
Here is the linker eror I get:

singleton error LNK2001: unresolved external symbol "private: static
class Test * Singleton<class Test>::instance _"

Can anyone spot where I'm going wrong?


I don't see where Singleton<Test> ::instance_ is being instanciated.

-Brian

Its been done in the instance method (T& Singleton<T>::i nstance())


My bad. Thanks for the correction guys. The ff file scope definition
fixed it:

template <class T>
T * Singleton<T>::i nstance_ = NULL ;

Aug 31 '05 #6

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

Similar topics

36
6397
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
8
5744
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <title>Untitled</title>
1
1612
by: Stewart Rogers | last post by:
Hi all, I have been working on an ASP.NET application that is a kind of wizard ( a list of sequential pages ). We built that application for the CLIENT-A and it worked fine. After six months CLIENT-B came along and and requested the same application but with little bit different requirements (both in terms of the front-end and back-end processing), so we had to create a new application for that client. Now came CLIENT-C !!!!!! I am...
8
1617
by: Valery | last post by:
hi All, how to make a member function, which is virtual not for a single object, but for the *pair* of objects?.. Here goes the skeleton of the code, which should ideally print "ABCD": ------------------------------------ class A {}; struct A1 : public A {} a1;
19
6786
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has one, and only one parent. The depth should not exceed 6 or 7 levels. The initial import will have about 6 million leaves, and 3 million branches. I would expect the leaves to grow significantly, in number easily tripling. However, the branches will...
81
7344
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
17
2412
by: Peter Oliphant | last post by:
In the 'old days', we could create a pointer to an instance of a variable like so: int i = 58 ; int* i_ptr = &i ; int j = *i_ptr ; // j = 58 Now, in /clr how do we do the same? That is, if I replace '*' with '^' what do I replace '&' with to generate a 'pointer' (is 'x^' called a 'reference' per chance?) to the instance? That is:
206
8368
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
11
2173
by: rogo | last post by:
Ok, when I began to implement it, I thought it should be straightforward and easy. I'm not sure whats wrong with the following code: class A { public: A() {} int get() {
0
9576
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
9407
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,...
0
10017
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9961
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,...
1
7384
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
6656
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5283
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3932
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 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.