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? 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
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())
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
> 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
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 ; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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":...
|
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...
|
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...
|
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,...
|
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...
|
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()
{
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
| |