Template metaprogramming forward declaration

Discussion in 'C++' started by steprobe, Apr 7, 2010.

  1. steprobe

    steprobe Guest

    Hi guys,
    If I have a class like the following:

    template<int Degree>
    class MegaClass
    {
    .....
    };

    And then I have another class in a seperate header file, myheader.h:

    class AnotherClass
    {
    private:
    MegaClass<2> theClass;
    };

    What is the syntax to forward declare MegaClass<2> in myheader.h? I have
    tried a few different ways, none of which have worked, and I couldn't
    find anything on google.

    Thanks,
    Stephen
     
    steprobe, Apr 7, 2010
    #1
    1. Advertising

  2. * steprobe:
    >
    > If I have a class like the following:
    >
    > template<int Degree>
    > class MegaClass
    > {
    > .....
    > };
    >
    > And then I have another class in a seperate header file, myheader.h:
    >
    > class AnotherClass
    > {
    > private:
    > MegaClass<2> theClass;
    > };
    >
    > What is the syntax to forward declare MegaClass<2> in myheader.h? I have
    > tried a few different ways, none of which have worked, and I couldn't
    > find anything on google.


    Since you have a MegaClass<2> data member a forward declaration isn't enough.
    For a declaration of a data member of type T, sizeof(T) must be well-defined.
    So, just #include the header that defines the class template.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Apr 7, 2010
    #2
    1. Advertising

  3. steprobe

    steprobe Guest

    >
    > Since you have a MegaClass<2> data member a forward declaration isn't
    > enough. For a declaration of a data member of type T, sizeof(T) must be
    > well-defined. So, just #include the header that defines the class template.


    Yes, of course. My mistake. If I change that member to a pointer how
    might I forward declare it?
     
    steprobe, Apr 7, 2010
    #3
  4. steprobe wrote:
    >>
    >> Since you have a MegaClass<2> data member a forward declaration isn't
    >> enough. For a declaration of a data member of type T, sizeof(T) must be
    >> well-defined. So, just #include the header that defines the class
    >> template.

    >
    > Yes, of course. My mistake. If I change that member to a pointer how
    > might I forward declare it?



    Like this:

    template< int Degree >
    class MegaClass;
     
    Vladimir Jovic, Apr 7, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jobseeker
    Replies:
    2
    Views:
    1,095
    DarkSpy
    Oct 15, 2003
  2. Nobody
    Replies:
    2
    Views:
    49,050
    Nobody
    Feb 12, 2004
  3. qazmlp
    Replies:
    1
    Views:
    605
    Jonathan Turkanis
    Feb 15, 2004
  4. verec
    Replies:
    2
    Views:
    1,047
    verec
    Jun 25, 2005
  5. Jure Erznožnik

    Forward template declaration problem

    Jure Erznožnik, Mar 12, 2011, in forum: C++
    Replies:
    4
    Views:
    728
    Jure Erznožnik
    Mar 12, 2011
Loading...

Share This Page