Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Self reference in Template class? (http://www.velocityreviews.com/forums/t621162-self-reference-in-template-class.html)

nw 06-19-2008 01:13 PM

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!

Fei Liu 06-19-2008 01:24 PM

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

Puppet_Sock 06-19-2008 01:26 PM

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

nw 06-19-2008 01:55 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57