Self reference in Template class?

Discussion in 'C++' started by nw, Jun 19, 2008.

  1. nw

    nw Guest

    Hi comp.lang.c++,

    I have the following header (simple.h):

    #ifndef SIMPLE
    #define SIMPLE

    template<class _prec=double>
    class Simple {

    public:

    Simple() {
    }

    Simple<_prec> next;
    };

    #endif

    and c++ file:

    #include <iostream>
    #include "simple.h"

    using namespace std;

    int main() {
    Simple<double> s;
    }


    Compilation gives me the error:

    simple.h: In instantiation of 'Simple<double>':
    simple.cpp:7: instantiated from here
    simple.h:12: error: 'Simple<_prec>::next' has incomplete type
    simple.h:5: error: declaration of 'class Simple<double>'

    under g++ 4.1.2. I guess I need to add a prototype for Simple, but I
    can't quite see how to do this for a template class. Any help
    appreciated!
     
    nw, Jun 19, 2008
    #1
    1. Advertising

  2. nw

    Fei Liu Guest

    nw wrote:
    > Hi comp.lang.c++,
    >
    > I have the following header (simple.h):
    >
    > #ifndef SIMPLE
    > #define SIMPLE
    >
    > template<class _prec=double>
    > class Simple {
    >
    > public:
    >
    > Simple() {
    > }
    >
    > Simple<_prec> next;
    > };
    >
    > #endif
    >
    > and c++ file:
    >
    > #include <iostream>
    > #include "simple.h"
    >
    > using namespace std;
    >
    > int main() {
    > Simple<double> s;
    > }
    >
    >
    > Compilation gives me the error:
    >
    > simple.h: In instantiation of 'Simple<double>':
    > simple.cpp:7: instantiated from here
    > simple.h:12: error: 'Simple<_prec>::next' has incomplete type
    > simple.h:5: error: declaration of 'class Simple<double>'
    >
    > under g++ 4.1.2. I guess I need to add a prototype for Simple, but I
    > can't quite see how to do this for a template class. Any help
    > appreciated!


    You cannot do this. Declare next as a pointer or reference type (and
    make sure the pointee is always valid of course during runtime).

    Fei
     
    Fei Liu, Jun 19, 2008
    #2
    1. Advertising

  3. nw

    Puppet_Sock Guest

    On Jun 19, 9:13 am, nw <> wrote:
    > Hi comp.lang.c++,
    >
    > I have the following header (simple.h):
    >
    > #ifndef SIMPLE
    > #define SIMPLE
    >
    > template<class _prec=double>
    > class Simple {
    >
    > public:
    >
    >   Simple() {
    >   }
    >
    >   Simple<_prec> next;
    >
    > };
    >
    > #endif

    [snip]

    Um. Is that going to work?

    You make an object of type Simple<x>. It has a data member
    of type Simple<x>. That data member has a data member of
    type Simple<x>. And so on. Eventually the two little girls
    from "The Shining" come out and ask you to come play with
    them "forever and ever and ever."

    Possibly you want a pointer as data member?
    Socks
     
    Puppet_Sock, Jun 19, 2008
    #3
  4. nw

    nw Guest

    > You make an object of type Simple<x>. It has a data member
    > of type Simple<x>. That data member has a data member of
    > type Simple<x>. And so on. Eventually the two little girls
    > from "The Shining" come out and ask you to come play with
    > them "forever and ever and ever."
    >
    > Possibly you want a pointer as data member?


    uh yea, I didn't think this though.

    Definitely don't want to invoke room 237, will refactor.
     
    nw, Jun 19, 2008
    #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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    624
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    624
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    432
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    406
    falcon
    Jul 31, 2005
  5. Replies:
    6
    Views:
    127
    Vivek
    Jan 25, 2006
Loading...

Share This Page