home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19759 < prev    next >
Encoding:
Text File  |  1993-01-22  |  3.1 KB  |  109 lines

  1. Xref: sparky comp.lang.c++:19759 comp.sys.sgi:19044
  2. Newsgroups: comp.lang.c++,comp.sys.sgi
  3. Path: sparky!uunet!pipex!doc.ic.ac.uk!cc.ic.ac.uk!imperial.ac.uk!vulture
  4. From: vulture@imperial.ac.uk (Thomas Sippel - Dau)
  5. Subject: Delayed Instantiation with Templates - How ?
  6. Message-ID: <1993Jan22.164610.8348@cc.ic.ac.uk>
  7. Followup-To: poster
  8. Sender: vulture@carrion.cc.ic.ac.uk (Thomas Sippel - Dau)
  9. Nntp-Posting-Host: cscgc
  10. Reply-To: cmaae47@imperial.ac.uk
  11. Organization: Imperial College of Science, Technology and Medicine
  12. Date: Fri, 22 Jan 93 16:46:10 GMT
  13. Lines: 94
  14.  
  15. Hello all,
  16.  
  17. having gotten our shiny new C++ compiler with all those important facilities 
  18. we thought our problems would be over. In fact, they haven't started yet.
  19.  
  20. We try to use templates and hit the old linked list problem: you can't use
  21. a type before its declared, so you can't have in an object type definition
  22. a pointer to such.
  23.  
  24. The program below does not do much, but shows our problem. We got the 
  25. following error messages:
  26.  
  27. zorn$ CC shape.C
  28. "shape.C", line 21: error: new shape; shape is undefined
  29. "shape.C", line 21:      error detected during the instantiation of List <shape>
  30. "shape.C", line 32:     is the site of the instantiation
  31. "shape.C", line 5: error:  shape undefined, size not known
  32. "shape.C", line 5:       error detected during the instantiation of List <shape>
  33. "shape.C", line 32:     is the site of the instantiation
  34. "shape.C", line 5: warning: delete shape ( shape not defined)
  35.   
  36. zorn$ versions c++
  37. I  c++                  12/17/92  C++, 3.0
  38. I  c++.hdr              12/17/92  C++ Headers
  39. I  c++.hdr.lib          12/17/92  C++ Library Headers
  40. I  c++.opt              12/17/92  C++ Optional
  41. I  c++.opt.gifts        12/17/92  C++ Gifts
  42. I  c++.sw               12/17/92  C++ Software
  43. I  c++.sw.c++           12/17/92  C++ Compiler (version 3.0)
  44. I  c++.sw.lib           12/17/92  C++ Libraries
  45.  
  46. zorn$ uname -a
  47. IRIX zorn 4.0.5F 08280217 IP20 (N.B. IP7 and IP12 - same difference)
  48.  
  49. We did RTFM and saw that what we really want is to delay instantiation of
  50. the class shape, but we could not quite get the idea how. Any help will be 
  51. greatly appreciated.
  52.  
  53. Thanks in advance.                    Thomas
  54.  
  55. zorn$ cat shape.C
  56. // The general-purpose generic List type
  57.  
  58. template<class T>
  59. class List
  60. {
  61.  
  62. private:
  63.  
  64.     // Vector of values
  65.     T* v;
  66.     
  67.     // Number of elements in above vector
  68.     int sz;
  69.  
  70. public:
  71.  
  72.     // Construct with length specified
  73.     List(const int sl)
  74.     {
  75.         register int s = sl;
  76.         if (s>0) v = new T[long(sz=s)];
  77.         else { v = 0; sz = 0; }
  78.     }
  79.  
  80.     // Destroy list elements
  81.     ~List() { delete[] v; }
  82.  
  83. };
  84.  
  85. class shape;
  86.  
  87. typedef List<shape> shapeList;
  88.  
  89.  
  90. class shape
  91. {
  92.         void subShapes(shapeList&){};
  93. };
  94.  
  95. main()
  96. {
  97.     List<shape> s(2);
  98. }
  99.  
  100. Keywords: 
  101.  
  102.  
  103. -- 
  104. *** This is the operative statement, all previous statements are inoperative.
  105. *   email: cmaae47 @ ic.ac.uk (Thomas Sippel - Dau) (uk.ac.ic on Janet)
  106. *   voice: +44 71 589 5111 x4937 or 4934 (day), or +44 71 823 9497 (fax)
  107. *   snail: Imperial College of Science, Technology and Medicine
  108. *   The Center for Computing Services, Kensington SW7 2BX, Great Britain
  109.