home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:19759 comp.sys.sgi:19044
- Newsgroups: comp.lang.c++,comp.sys.sgi
- Path: sparky!uunet!pipex!doc.ic.ac.uk!cc.ic.ac.uk!imperial.ac.uk!vulture
- From: vulture@imperial.ac.uk (Thomas Sippel - Dau)
- Subject: Delayed Instantiation with Templates - How ?
- Message-ID: <1993Jan22.164610.8348@cc.ic.ac.uk>
- Followup-To: poster
- Sender: vulture@carrion.cc.ic.ac.uk (Thomas Sippel - Dau)
- Nntp-Posting-Host: cscgc
- Reply-To: cmaae47@imperial.ac.uk
- Organization: Imperial College of Science, Technology and Medicine
- Date: Fri, 22 Jan 93 16:46:10 GMT
- Lines: 94
-
- Hello all,
-
- having gotten our shiny new C++ compiler with all those important facilities
- we thought our problems would be over. In fact, they haven't started yet.
-
- We try to use templates and hit the old linked list problem: you can't use
- a type before its declared, so you can't have in an object type definition
- a pointer to such.
-
- The program below does not do much, but shows our problem. We got the
- following error messages:
-
- zorn$ CC shape.C
- "shape.C", line 21: error: new shape; shape is undefined
- "shape.C", line 21: error detected during the instantiation of List <shape>
- "shape.C", line 32: is the site of the instantiation
- "shape.C", line 5: error: shape undefined, size not known
- "shape.C", line 5: error detected during the instantiation of List <shape>
- "shape.C", line 32: is the site of the instantiation
- "shape.C", line 5: warning: delete shape ( shape not defined)
-
- zorn$ versions c++
- I c++ 12/17/92 C++, 3.0
- I c++.hdr 12/17/92 C++ Headers
- I c++.hdr.lib 12/17/92 C++ Library Headers
- I c++.opt 12/17/92 C++ Optional
- I c++.opt.gifts 12/17/92 C++ Gifts
- I c++.sw 12/17/92 C++ Software
- I c++.sw.c++ 12/17/92 C++ Compiler (version 3.0)
- I c++.sw.lib 12/17/92 C++ Libraries
-
- zorn$ uname -a
- IRIX zorn 4.0.5F 08280217 IP20 (N.B. IP7 and IP12 - same difference)
-
- We did RTFM and saw that what we really want is to delay instantiation of
- the class shape, but we could not quite get the idea how. Any help will be
- greatly appreciated.
-
- Thanks in advance. Thomas
-
- zorn$ cat shape.C
- // The general-purpose generic List type
-
- template<class T>
- class List
- {
-
- private:
-
- // Vector of values
- T* v;
-
- // Number of elements in above vector
- int sz;
-
- public:
-
- // Construct with length specified
- List(const int sl)
- {
- register int s = sl;
- if (s>0) v = new T[long(sz=s)];
- else { v = 0; sz = 0; }
- }
-
- // Destroy list elements
- ~List() { delete[] v; }
-
- };
-
- class shape;
-
- typedef List<shape> shapeList;
-
-
- class shape
- {
- void subShapes(shapeList&){};
- };
-
- main()
- {
- List<shape> s(2);
- }
-
- Keywords:
-
-
- --
- *** This is the operative statement, all previous statements are inoperative.
- * email: cmaae47 @ ic.ac.uk (Thomas Sippel - Dau) (uk.ac.ic on Janet)
- * voice: +44 71 589 5111 x4937 or 4934 (day), or +44 71 823 9497 (fax)
- * snail: Imperial College of Science, Technology and Medicine
- * The Center for Computing Services, Kensington SW7 2BX, Great Britain
-