![]() |
Self reference in Template class?
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! |
Re: Self reference in Template class?
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 |
Re: Self reference in Template class?
On Jun 19, 9:13*am, nw <n...@soton.ac.uk> 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 |
Re: Self reference in Template class?
> 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. |
| All times are GMT. The time now is 08:30 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.