Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
About Us Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old September 29th, 2003   #1 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,537
dumb c++ question

I know this is stupi but i dunno how to do it

i have this little template

template<class T>
struct nodoLista
{
T info;
nodoLista *siguiente;
};

typedef nodoLista* ApuntadorNodo; //the compiler marks error here

How can i correctly do a pointer of that type of template structure
__________________


Proto is offline   Reply With Quote
Old September 29th, 2003   #2 (permalink)
Ramsus K
 
Join Date: Mar 2003
Location: Oklahoma
Posts: 461
Re: dumb c++ question

Since a template isn't a real implementation, but a skeleton for one that can be generated by the compiler for every type of object you use it with, I don't think you can typedef a template. Consider using void *T with a regular struct (no templates), or implementing a class template for the Node that makes use of references instead of a struct template.
RamsusX is offline   Reply With Quote
Old September 29th, 2003   #3 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,537
Re: dumb c++ question

well then i guess it would go in the lines of this

template[class T]
class nodoLista
{
T info;
nodoLista *siguiente;
};

still... how can i make a pointer to a template class...would it be (nodolista*)[T]

with <> instead of []
__________________


Proto is offline   Reply With Quote
Old September 29th, 2003   #4 (permalink)
Ramsus K
 
Join Date: Mar 2003
Location: Oklahoma
Posts: 461
Re: dumb c++ question

You can't make a type that is a pointer to a class template. You have to go about this in a different manner.

This is because you need to use a template in the manner:
templateClassName&lt;type&gt; name;

So we use:
Code:
template &lt;class T&gt;
struct nodoLista
{
  T *info;
  nodoLista &lt;T&gt; *siguiente;
};
But if you want to implement a linked list in C++ using templates, then use a class AS a class, complete with members and methods and fully encapsulated.

Do this by implementing a Node class, then creating a List class that maintains a linked list of Node's.

*Edit: For creating nodes, use code like the following:
Code:
/* ...  */
    int *x = new int;
    *x = 4;
    int y = 2;
    nodoLista &lt;int&gt; *nodo = new nodoLista&lt;int&gt;;
    nodo->info = x;
    nodo->info = &y;
    delete nodo;
    delete x;
/* ... */

Last edited by RamsusX; September 29th, 2003 at 04:51. Reason: Fixed an accidental bug.
RamsusX is offline   Reply With Quote
Old September 29th, 2003   #5 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,537
Re: dumb c++ question

Ok, thx, sorry for being so annoying, but is there a way for the class to wait for the execution to assign the type to a template variable, or do i have to define it on the code?
__________________


Proto is offline   Reply With Quote
Old September 29th, 2003   #6 (permalink)
Ramsus K
 
Join Date: Mar 2003
Location: Oklahoma
Posts: 461
Re: dumb c++ question

You have to assign a type to the template in the code so that the compiler knows what type the template is when compiling.

However, if typesafety isn't an issue, then you can also use void * pointers to point to objects of any type.

Code:
struct nodoLista
{
  void *info;
  nodoLista *siguente;
};

/* ... */
  int p = 2;
  char c = 'A';
  nodoLista *nodo = new nodoLista;
  nodo->info = &p;
  nodo->info = &c;
  delete nodo;
/* ... */
RamsusX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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 On


All times are GMT. The time now is 02:13.

© 2006 - 2008 Emu Forums | About Emu Forums | Legal | A member of the Crowdgather Forum Community


Powered by vBulletin® Version 3.7.0 Release Candidate 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5