473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to use typedef together with template ?

21
I have some class,e.g
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class node{
  3.   T key;
  4.   node* next;
  5. };
  6.  
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 .
Aug 1 '07 #1
7 1765
JosAH
11,448 Expert 8TB
I have some class,e.g
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class node{
  3.   T key;
  4.   node* next;
  5. };
  6.  
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
Aug 1 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. typedef node<T>* nodeptr;       //error
  2. typedef node<int>* nodeptr;     //OK
  3.  
Aug 1 '07 #3
JosAH
11,448 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. typedef node<T>* nodeptr;       //error
  2. typedef node<int>* nodeptr;     //OK
  3.  
Darn, I missed this one completely; thanks for helping out.

kind regards,

Jos
Aug 1 '07 #4
VanKha
21
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 ?
Aug 1 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class node{
  3.   T key;
  4.   node* next;
  5. };
  6.  
  7.  
  8. template<class T>
  9. struct List
  10. {
  11.     node<T>* start;
  12.     node<T>* end;
  13.     node<T>* current;
  14. };
  15.  
  16. int main()
  17. {
  18.      List<int> mylist;
  19. }
  20.  
This compile and links just fine.

No typedef.
Aug 1 '07 #6
VanKha
21
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 ?
Aug 2 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 2 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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>
5
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...
7
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 ...
2
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...
2
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...
6
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...
5
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...
5
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...
12
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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,...

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.