I have some class,e.g -
template<class T>
-
class node{
-
T key;
-
node* next;
-
};
-
and I want to define a pointer to the class node as a type . How to declare ?
I try typedef node<T>* nodeptr but it doesn't work,there'll be error .
7 1765
I have some class,e.g -
template<class T>
-
class node{
-
T key;
-
node* next;
-
};
-
and I want to define a pointer to the class node as a type . How to declare ?
I try typedef node<T>* nodeptr but it doesn't work,there'll be error .
What is the error exactly? That error surprises me a bit ...
kind regards,
Jos
A typedef creates a new name for an existing type. A class template is not a type. Rather, it is a pattern for a type. Not until you specialize it can you typedef it: -
typedef node<T>* nodeptr; //error
-
typedef node<int>* nodeptr; //OK
-
A typedef creates a new name for an existing type. A class template is not a type. Rather, it is a pattern for a type. Not until you specialize it can you typedef it: -
typedef node<T>* nodeptr; //error
-
typedef node<int>* nodeptr; //OK
-
Darn, I missed this one completely; thanks for helping out.
kind regards,
Jos
Thanks a lot . But if I have to declare a specific T , another problem arises . It is because of the fact that I just compiled successfully a program with the class node and a class list using that "node" . I do like this
class node...
typedef node* nodeptr;
class list
//..using nodeptr many times here
and it compiles well !
Then I decide to use the template . And so , I must have node<T>* as nodeptr to make use of it in the list class . If I cannot declare nodeptr like the wrong syntax(!!!) , how can I write the list class ?Don't tell me that just go around the program and replace nodeptr with node<T>* ! Is there anyway to come around this problem ?
Then I decide to use the template . And so , I must have node<T>* as nodeptr to make use of it in the list class .
You mean like this: -
template<class T>
-
class node{
-
T key;
-
node* next;
-
};
-
-
-
template<class T>
-
struct List
-
{
-
node<T>* start;
-
node<T>* end;
-
node<T>* current;
-
};
-
-
int main()
-
{
-
List<int> mylist;
-
}
-
This compile and links just fine.
No typedef.
Yes,but the thing is I have written nodeptr in my program instead of node<T>* ! I mean that at first I wrote like that and after updating with the template method pattern , I had to change all to node<T>* ? If the typedef could have worked , I have to do nothing but now that the typedef is wrong,I have to change all !! Sorry,just because I think that it's so unreasonable . Have anybody encounter the problem like me ? I think the answer must be yes,because just like me now,everybody must have the 'newbie in c++' period , so after writing the code without template,then reading textbooks,seeing the usefulness,then attemping to apply the template method,you'll expect that just change from typedef node* nodeptr to typdef node<T>* nodeptr and no more ! It's really natural expectation,isn't it ?
It's expected with a typedef that you already have a type. A template is not a type.
Ultimately, you will need to change your code so it is acceptable to the compiler. You aren't the first one who had to ditch a lot of work over an assumption.
The lesson here is to write only a little code and then compile. Once it compiles, then write a little more. That way you always have code that compiles and any errors will usually be in the code that was last added.
Writing code for a long time and then doing a compile is a recipe for rework and frustration.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Arkadiy Vertleyb |
last post by:
Hi all,
I am having a problem trying to overload a function template, based on
a typedef, such as:
template<class T>
struct A
{};
template<class T>
|
by: Roger Leigh |
last post by:
Although I've got over most of my template-related problems, I'm
having trouble when I started to use default template parameters.
For template type T, I've typedef'd this as object_type and then...
|
by: Tony Johansson |
last post by:
Hello Experts!
I have the following Array template class see below.
I execute these three statements
statement 1: Array<int> x(5);
statement 2: cin >>x;
statement 3: Array<int>::element_type ...
|
by: PengYu.UT |
last post by:
I have the following sample program, which can convert function object
with 1 argument into function object with 2 arguments. It can also do +
between function object of the same type.
The last...
|
by: Patrick Kowalzick |
last post by:
Hello NG,
sorry to bother again, but I am a lit surprised that I got no answer on my
post (attached below).
So I refined the code a little bit :-).
If there is a typedefed class X inside a...
|
by: Alex |
last post by:
Hello people,
I am getting errors from VS2003 when working with typedef'ed types.
For example, assume that I have a type T, defined in a 3rd party include file based on some condition
#if...
|
by: jimmy |
last post by:
I am trying to simulate typedef template similar to the suggestion of
Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm
However when implementing typedef templates according...
|
by: Alan Woodland |
last post by:
Hi,
I don't think I can't do this directly with standard C++, I've tried all
the ways I could think of that make sense and then some. I was wondering
if someone had a genius idea how I could...
|
by: aaragon |
last post by:
Hello all. I have a simple question that seems trivial but I can't
make it to work. I have a class that takes as a template argument,
another class. The idea is as follows:
#include...
|
by: Adam Nielsen |
last post by:
Hi everyone,
Yet another syntax problem that's baffling me with templates. I want to
instantiate a template with a single parameter as per normal, however
the parameter is actually a template...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
| |