473,793 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Self reference in Template class?

nw
Hi comp.lang.c++,

I have the following header (simple.h):

#ifndef SIMPLE
#define SIMPLE

template<class _prec=double>
class Simple {

public:

Simple() {
}

Simple<_precnex t;
};

#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!
Jun 27 '08 #1
1 2491
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<_precnex t;
};

#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
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3680
by: SpeedBump | last post by:
This is mostly just another "gee it would be nice if it had X" post. Recently I have come across two separate problems which both exhibit the need for a way to self reference when instantiating a template. Example #1: B-trees While implementing a B-Tree I ended up with an (abbreviated) node structure looking something like this:
2
2108
by: Agent Mulder | last post by:
Hi group, I try to get a reference to a template. In the template, a virtual function is declared. Later in the program, I inherit from classes created by the template. So the template is the base of them all. Because I don't know what classes I am dealing with, I try to get a reference to the base class. Since the base class is a template instaniation, I cannot find a common type for all classes that
1
2897
by: Greg Phillips | last post by:
Hello everyone, I have read the section on templates in The C++ Programming Language 3rd edition 3 times over the last 4 days. Still I am stumped. I'll explain a bit about what I am doing before asking the question so as you can understand what I am trying to accomplish. Any help would be much appreciated.
8
1667
by: JKop | last post by:
Having decided not to use macros at all in designing my reseatable references, and having looked through the list of overloadable operators and combinations of them, I've settled on the following syntax: Reseat >> kref = moon; Where "kref" is the reseatable reference and "moon" is the new object it'll refer to. Overall, once a reseatable reference has been defined, I want it to act
20
2869
by: Wayne Sutton | last post by:
OK, I'm a newbie... I'm trying to learn Python & have had fun with it so far. But I'm having trouble following the many code examples with the object "self." Can someone explain this usage in plain english? Thanks, Wayne
34
7188
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
3
2489
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
7
1777
by: comp.lang.php | last post by:
<? class EditResultGenerator extends MethodGeneratorForActionPerformer { /** * Create an instance of itself if $this does not yet exist as an EditResultGenerator class object * * @access private * @see is_class */
1
1494
by: PeterAPIIT | last post by:
What is template template arguments and template typename arguments ? The reason i write in policy based design is because this is the requirement of the assignment. My code so far: #include "StoragePolicy.h"
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.