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

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>::instance() {
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 1444
> 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>::instance() {
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>::instance() {
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>::instance())

Aug 31 '05 #3
Alfonso Morra wrote:

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


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>::instance() {
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>::instance())


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>::instance() {
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>::instance())


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

template <class T>
T * Singleton<T>::instance_ = NULL ;

Aug 31 '05 #6

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

Similar topics

36
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...
8
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...
1
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...
8
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":...
19
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...
81
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...
17
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,...
206
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...
11
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.