|
|
|||||||
| About Us | Register | FAQ | Members List | Calendar | Mark Forums Read |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() 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
__________________
|
|
|
|
|
|
#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.
|
|
|
|
|
|
#3 (permalink) |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() 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 []
__________________
|
|
|
|
|
|
#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<type> name; So we use: Code:
template <class T>
struct nodoLista
{
T *info;
nodoLista <T> *siguiente;
};
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 <int> *nodo = new nodoLista<int>;
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. |
|
|
|
|
|
#5 (permalink) |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() 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?
__________________
|
|
|
|
|
|
#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;
/* ... */
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|