home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13299 < prev    next >
Encoding:
Text File  |  1992-09-04  |  3.3 KB  |  120 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!sunic!ugle.unit.no!sigyn.idt.unit.no!bjornmu
  3. From: bjornmu@idt.unit.no (Bj|rn P. Munch)
  4. Subject: Re: Problem with templates...
  5. Message-ID: <1992Sep4.154825.12584@ugle.unit.no>
  6. Sender: news@ugle.unit.no (NetNews Administrator)
  7. Organization: Div. of CS & Telematics, Norwegian Institute of Technology
  8. References:  <Bu0xvC.6np@usenet.ucs.indiana.edu>
  9. Date: Fri, 4 Sep 92 15:48:25 GMT
  10. Lines: 108
  11.  
  12. In article <Bu0xvC.6np@usenet.ucs.indiana.edu>, nsundare@silver.ucs.indiana.edu (Neelakantan Sundaresan) writes:
  13. |> 
  14. |> I am trying to separate compile template definitions and use.
  15. |> For instance,
  16. |>  file1.c has
  17. |>  -----
  18. |> 
  19. |> #include <stdio.h>
  20. |> #include <stdarg.h>
  21. |> #include "file1.h"
  22. |> 
  23. |> template <class T> XT<T>::Xtest(T t)
  24. |> { printf("HEE\n");
  25. |>   return 0;
  26. |> }
  27. |> template <class T> XT<T>::XT(){}
  28. |> 
  29. |>  file2.c has
  30. |>  ------
  31. |> 
  32. |> #include <stdio.h>
  33. |> #include <stdarg.h>
  34. |> #include "file1.h"
  35. |> 
  36. |> main()
  37. |> {
  38. |>   typedef XT<int> Y;
  39. |>   Y x;
  40. |>   x.Xtest(4);
  41. |> }
  42. |> 
  43. |>   file1.h has
  44. |>   -------
  45. |> 
  46. |> template <class T> class XT {
  47. |>  public:
  48. |>    XT();
  49. |>    Xtest(T);
  50. |> };
  51. |> 
  52. |> 
  53. |> ------------ On compiling with g++ and linking I get the error
  54. |>  undefined symbol:
  55. |>      ___t2XT1Zi 
  56. |>          _Xtest__t2XT1Zii 
  57. |> 
  58. |> But if I have file1.c and file2.c in the same file, everything seems
  59. |> to be okay.
  60. |> 
  61. |> 
  62. |> Any ideas as to what is going on?
  63. |> thanks
  64. |> Neel.
  65.  
  66. Yes, I've been struggling with a similar problem.  I assume you want
  67. to have the member functions compiled in file.c only.
  68.  
  69. Well, that does not happen.  You "instantiate" the template in
  70. file2.c, but there the compiler doesn't have the definitions (bodies)
  71. of the member functions available.  In file.c it's the opposite; you
  72. have the body templates, but don't create any instances of them.
  73.  
  74. What you need to do, is to #include file1.c also into file2.c; then it
  75. should work.
  76.  
  77. You'll have to include file.c also into all other files that use
  78. instances of XT.  If you want to avoid this, and have the functions
  79. for XT<int> compiled only in one file (but use it in other files), try
  80. this:
  81.  
  82. In file1.h, say:
  83.  
  84. > #ifdef __GNUG__
  85. > #pragma interface
  86. > #endif
  87.  
  88. I'm not 100% sure that is required, but what _is_ required, is to
  89. include this in the .c file where you want to create the functions:
  90.  
  91. > #ifdef __GNUG__
  92. > #pragma implementation "file1.h"
  93. > #endif
  94. > #include  "file1.c"
  95.  
  96. This will make g++ spit out global functions, not local (static) ones.
  97. You can use this file to instantiate all the XT's you need, e.g. by:
  98.  
  99. > typedef XT<int>  intXT;
  100. > typedef XT<float>  floatXT;
  101. > typedef XT<char *>  strXT;
  102.  
  103. , and you will have all the functions made for you.  In other files,
  104. you can use these by including file1.h, but you don't have to include
  105. file1.c too.
  106.  
  107. Hope this was comprehensible?  My earlier post about the same topic
  108. was probably too cryptic, sorry.   :-|
  109.  
  110. Disclaimer: I haven't read this in docs, but found it out "the hard
  111. way".  Works for me with g++ v 2.2.2 on a Sun4 running SunOS 4.1.2.
  112.  
  113. - BugWriter
  114.  
  115. ---
  116. Bj|rn P. Munch               | Dept. of Comp. Science & Telematics,
  117. Bjoern.P.Munch@idt.unit.no   | Norwegian Institute of Technology (NTH),
  118. PhD Student                  | N-7034 Trondheim, Norway
  119. Univeristy of Trondheim      | Fingerable addr:  bjornmu@multe.idt.unit.no
  120.