Build errors on "trivial" proof of concept design pattern

A

Alfonso Morra

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?
 
B

BigBrian

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
 
A

Alfonso Morra

BigBrian said:
Where is this static member instanciated?




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

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

Karl Heinz Buchegger

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

No.
What BigBrian ment is:
You *declared* a variable instance_, here:

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.
 
B

BigBrian

BigBrian said:
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
 
A

Alfonso Morra

Alfonso said:
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 ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top