C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem using static member of template class...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Sylvain
Guest





PostPosted: Thu Sep 11, 2003 11:44 pm    Post subject: Problem using static member of template class... Reply with quote



Hi All,

I am trying to compile under HP-UX using g++ v 3.2.1...
An example with static member data of template class...
It does not work and I get during the link the following error:
/usr/ccs/bin/ld: Unsatisfied symbols:
My_super_class<char*>::kind_string (data)
My_super_class<float>::kind_string (data)
My_super_class<int>::kind_string (data)
collect2: ld returned 1 exit status
However the example works fine on other systems (Solaris, Djgpp) with
same version of GCC...

I wondered if there would be a workaround this problem...

Thanks

Sylvain


HERE IS THE FAILING CODE:


# include <iostream>

using namespace std;

template <class T>
class My_super_class
{
public:
int value;
T payload;
My_super_class(int _value = 0):
value (_value)
{
}

static const char * kind_string;
};

template <class T>
const char * My_super_class<T>::kind_string = "VERY KIND";

int main()
{
My_super_class<int> msci(3);
My_super_class<float> mscf(4);
My_super_class<char * > mscv(5);

cout << msci.kind_string << endl;
cout << mscf.kind_string << endl;
cout << mscv.kind_string << endl;





return 0;
}



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guy Harrison
Guest





PostPosted: Wed Sep 17, 2003 9:33 am    Post subject: Re: Problem using static member of template class... Reply with quote



Sylvain wrote:

Quote:
Hi All,

I am trying to compile under HP-UX using g++ v 3.2.1...
An example with static member data of template class...
It does not work and I get during the link the following error:
/usr/ccs/bin/ld: Unsatisfied symbols:
My_super_class<char*>::kind_string (data)
My_super_class<float>::kind_string (data)
My_super_class<int>::kind_string (data)
collect2: ld returned 1 exit status
However the example works fine on other systems (Solaris, Djgpp) with
same version of GCC...

I wondered if there would be a workaround this problem...

You can be more specific to the compiler.
Quote:

# include <iostream

using namespace std;

Plonk this in a header (eg: foo.h) ...

Quote:
template class My_super_class
{
public:
int value;
T payload;
My_super_class(int _value = 0):
value (_value)
{
}

static const char * kind_string;
};

.....and this into a source file (foo.cpp)...

Quote:
template const char * My_super_class

.....but change to...

<foo.cpp>
template <>
const char * My_super_class<int>::kind_string = "VERY KIND";

template <>
const char * My_super_class<float>::kind_string = "VERY KIND";

template <>
const char * My_super_class<char*>::kind_string = "VERY KIND";
</foo.cpp>

.....and obviously have foo.cpp be compiled like normal.

Quote:
int main()
{
My_super_class<int> msci(3);
My_super_class<float> mscf(4);
My_super_class<char * > mscv(5);

cout << msci.kind_string << endl;
cout << mscf.kind_string << endl;
cout << mscv.kind_string << endl;

.....and the linker should be able to find those three specialisations (in
your foo.o file).


--
Guy Harrison

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.