Topic: question on template.


Author: tlhol@ibm.net (Thomas Holaday)
Date: 1998/05/18
Raw View
[This followup was posted to comp.std.c++ and a copy was sent to the
cited author.]

In article <Pine.SUN.3.94.980512180726.12829A-100000@cuphy3>,
mfgu@cuphy3.phys.columbia.edu says...
>   template <class T>
>   class a{
>  ...
>  T m1;
>  void func_of_a(void){
>   m1.func_of_b();};
>  ...
>  };
> and
>   template <class T>
>   class b{
>  ......
>  T m2;
>  void func_of_b(void){...};
>  ......
>  };
> is it possible to instantiate template a of type, say, b<int>?

Here's how:

template <class T>
class a {
public:
 T m1;
 void afn();
};

template <class T>
class b {
public:
 T m2;
 void bfn() ;
};

template <class T>
void a<T>::afn() { m1.bfn(); }

template <class T>
void b<T>::bfn() { } ;

Your problem was trying to define the methods in the template class body.

-- Thomas


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ming Feng Gu <mfgu@cuphy3.phys.columbia.edu>
Date: 1998/05/12
Raw View
here is my question,
define two template classes a,b
  template <class T>
  class a{
 ...
 T m1;
 void func_of_a(void){
  m1.func_of_b();};
 ...
 };
and
  template <class T>
  class b{
 ......
 T m2;
 void func_of_b(void){...};
 ......
 };
is it possible to instantiate template a of type, say, b<int>?
a<(b<int>)> cause error flag at compiling time, while the following pass
the compile,
typedef b<int> int_b;
foo=a<int_b>;
but at linking time, func_of_b used in func_of_a is flagged as undefined
symbol.
is there anyway out of this? i appreciate any suggestions. please send me
an email as well while posting the reply.
ming gu



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]