Connecting Tech Pros Worldwide Forums | Help | Site Map

Self reference in Template class?

nw
Guest
 
Posts: n/a
#1: Jun 27 '08
Hi comp.lang.c++,

I have the following header (simple.h):

#ifndef SIMPLE
#define SIMPLE

template<class _prec=double>
class Simple {

public:

Simple() {
}

Simple<_precnext;
};

#endif

and c++ file:

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

using namespace std;

int main() {
Simple<doubles;
}


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
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Self reference in Template class?


nw wrote:
Quote:
Hi comp.lang.c++,
>
I have the following header (simple.h):
>
#ifndef SIMPLE
#define SIMPLE
>
template<class _prec=double>
class Simple {
>
public:
>
Simple() {
}
>
Simple<_precnext;
};
>
#endif
>
and c++ file:
>
#include <iostream>
#include "simple.h"
>
using namespace std;
>
int main() {
Simple<doubles;
}
>
>
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
Closed Thread