Topic: Template problem...


Author: gkm@magilla.cichlid.com (Greg McGary)
Date: Thu, 20 Oct 1994 02:34:58 GMT
Raw View
Hugues Marty <hugues@isoftfr.isoft.fr> wrote:
>I'd like to declare a class (which is a template) before it's really defined.

Here's what you want:

template<class T> class Base; // forward-declaration of template name

typedef Base<int> SubBase; // forward-declaration of an instantiation

class X {
 public:
  SubBase* sb;   // use of forward-declaration is OK
    // if it's a pointer or reference
};

 // sometime later...

template<class T>  // definition of the template.
class Base {   // instantiation of Base<int> now occurs
  T *t;
};

--gkm




Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 19 Oct 1994 19:44:12 GMT
Raw View
>>>>> Hugues Marty <hugues@isoftfr.isoft.fr> writes:

> What's wrong with that code? Do I really have to declare `typedef
> Base<int> SubBase;' from the beginning?

Yes.

Jason




Author: hugues@isoftfr.isoft.fr (Hugues Marty)
Date: 19 Oct 1994 08:29:07 GMT
Raw View
I'd like to declare a class (which is a template) before it's really defined.

This fails:
-------------------
template<class T>
class Base {
  T *t;
};

template<class T> class SubBase;
// `class SubBase;' alone fails too
// with error:
// tt.cc:14: conflicting types for `class Base<int>'
// tt.cc:6: previous declaration as `class SubBase'

class X {
 public:
  SubBase *sb;
};

typedef Base<int> SubBase; // this is line 14
-------------------
I get this error (using gcc 2.6.0):
tt.cc:14: warning: ANSI C++ forbids declaration `SubBase' with no type or storage class
tt.cc:14: parse error before `*'

What's wrong with that code? Do I really have to declare `typedef
Base<int> SubBase;' from the beginning?

Thanks,
hugues.
--
--
Hugues MARTY:  ISoft,  Chemin de Moulon,  F-91190 Gif-sur-Yvette,  France
e-mail: hugues@isoft.fr/phone: (33 1) 69 41 27 77/fax: (33 1) 69 41 25 32




Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Wed, 19 Oct 1994 14:25:35 GMT
Raw View
In article <HUGUES.94Oct19092907@isoftfr.isoft.fr>,
Hugues Marty <hugues@isoftfr.isoft.fr> wrote:
>I'd like to declare a class (which is a template) before it's really defined.
>
>template<class T> class SubBase;
>typedef Base<int> SubBase;

 The first declaration says that SubBase is a template. The second one
says that it is a class. It can't be both.
 -- Pete