Article View: gnu.gcc.help
Article #42837Re: External template class member functions in g++
From: Jimen Ching
Date: Sat, 28 Oct 1995 00:00
Date: Sat, 28 Oct 1995 00:00
54 lines
1758 bytes
1758 bytes
Henty Waker (henty@foxbat.sur.uct.ac.za) wrote:
>1) Defining member functions for a template class outside of header file:
>Does anyone know if I'm doing something wrong or how to get round this ?
Sorry, can't help you on this one. The -frepo doesn't help here, either.
>2) Using template functions in a class
>I can't seem to define member functions templates in a class. Again this
>program is just for eg:
Well, maybe if you could let us look at the actual code where you have
trouble, we can help you better?
>class test
>{
> void* ptr;
>public:
> template <class T> int setptr (T type_wanted, int size)
> { ptr = malloc (sizeof (T)*size); }
>};
>int main()
>{
> test object;
> int a;
> object.setptr (a, 10);
>}
But sir, why make 'ptr' a (void *)? Why not a (T *)? I.e.
template<class T>
class test
{
T *ptr;
public:
int setptr(int size)
{ ptr = new T[size]; }
};
int
main()
{
test<int> object;
object.setptr(10);
return 0;// memory leak!!!
}
This is an obvious solution. Since you didn't use it, there must be
some reason in your original code which prevented you from using this
class definition. Maybe if you described your problem, you might get
better results. Good luck!
--jc
--
Jimen Ching (WH6BRR) jching@aloha.com wh6brr@uhm.ampr.org
Technical Details
Message-ID:
<46sm76$mjb@news.aloha.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<46lq5g$ntn@ucthpx.uct.ac.za>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,7f638a5a4d0f3a29,start X-Google-Attributes: gid11318c,public From: jching@aloha.com (Jimen Ching) Subject: Re: External template class member functions in g++ Date: 1995/10/28 Message-ID: <46sm76$mjb@news.aloha.com> X-Deja-AN: 118261240 references: <46lq5g$ntn@ucthpx.uct.ac.za> organization: FlexNet Inc, HAWAII newsgroups: gnu.gcc.help