Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 12:05 PM
Alfonso Morra
Guest
 
Posts: n/a
Default 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?

  #2  
Old August 31st, 2005, 12:15 PM
BigBrian
Guest
 
Posts: n/a
Default Re: Build errors on "trivial" proof of concept design pattern

> Hi,[color=blue]
>
> 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_ ;[/color]

Where is this static member instanciated?
[color=blue]
> 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?[/color]

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

-Brian

  #3  
Old August 31st, 2005, 12:25 PM
Alfonso Morra
Guest
 
Posts: n/a
Default Re: Build errors on "trivial" proof of concept design pattern



BigBrian wrote:
[color=blue][color=green]
>>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_ ;[/color]
>
>
> Where is this static member instanciated?
>
>[color=green]
>> 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?[/color]
>
>
> I don't see where Singleton<Test>::instance_ is being instanciated.
>
> -Brian
>[/color]
Its been done in the instance method (T& Singleton<T>::instance())

  #4  
Old August 31st, 2005, 12:35 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: Build errors on "trivial" proof of concept design pattern

Alfonso Morra wrote:[color=blue]
>[color=green]
> >[/color]
> Its been done in the instance method (T& Singleton<T>::instance())[/color]

No.
What BigBrian ment is:
You *declared* a variable instance_, here:
[color=blue][color=green]
>>template <class T>
>>class Singleton {
>>
>> public:
>> static T& instance();
>> protected:
>> Singleton() {}
>>
>> private:
>> static T* instance_ ;[/color][/color]

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
kbuchegg@gascad.at
  #5  
Old August 31st, 2005, 12:45 PM
BigBrian
Guest
 
Posts: n/a
Default Re: Build errors on "trivial" proof of concept design pattern

> BigBrian wrote:[color=blue]
>[color=green][color=darkred]
> >>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_ ;[/color]
> >
> >
> > Where is this static member instanciated?
> >
> >[color=darkred]
> >> 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?[/color]
> >
> >
> > I don't see where Singleton<Test>::instance_ is being instanciated.
> >
> > -Brian
> >[/color]
> Its been done in the instance method (T& Singleton<T>::instance())[/color]

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

-Brian

  #6  
Old August 31st, 2005, 02:45 PM
Alfonso Morra
Guest
 
Posts: n/a
Default Re: Build errors on "trivial" proof of concept design pattern



Alfonso Morra wrote:
[color=blue]
>
>
> BigBrian wrote:
>[color=green][color=darkred]
>>> 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_ ;[/color]
>>
>>
>>
>> Where is this static member instanciated?
>>
>>[color=darkred]
>>> 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?[/color]
>>
>>
>>
>> I don't see where Singleton<Test>::instance_ is being instanciated.
>>
>> -Brian
>>[/color]
> Its been done in the instance method (T& Singleton<T>::instance())
>[/color]

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

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles