Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Self reference in Template class?

Reply
Thread Tools

Self reference in Template class?

 
 
nw
Guest
Posts: n/a
 
      06-19-2008
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!
 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      06-19-2008
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
 
Reply With Quote
 
 
 
 
Puppet_Sock
Guest
Posts: n/a
 
      06-19-2008
On Jun 19, 9:13*am, nw <(E-Mail Removed)> 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
 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      06-19-2008
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing self: if self is a tree how to set to a different self Bart Kastermans Python 6 07-13-2008 11:19 AM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) falcon Python 0 07-31-2005 05:41 PM
Re: __autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 2 07-12-2005 03:20 AM
Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code Ralf W. Grosse-Kunstleve Python 16 07-11-2005 09:28 PM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 18 07-11-2005 04:01 PM



Advertisments