Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Templated Linked List (Each element different type)

I am trying to create a templated linked list in C++, so I wrote this code to test whether one templated node could link to another templated node of different template type, but it gave me the error shown in comments. Any suggestions?

#include <iostream.h>

template <class T> Class Node
{
public:
T data;
Node * next;
}

void main()
{
Node<int> n1;
n1.data = 4;
Node<double> n2;
n2.data = 5.22;
n1.next = &n2; // cannot convert from 'class Node<double> *' to 'class Node<int> *'
}

~Mike
0
Mikendher
Asked:
Mikendher
  • 3
  • 2
  • 2
  • +1
1 Solution
 
jkrCommented:
Well, what should I say, the compiler is right :o)

How would you determine of what type the node 'next' points is anyway?
0
 
n_fortynineCommented:
#include <iostream.h>

template <class T> class Node
{
public:
        T data;
        void * next;
};

int main()
{
        Node<int> n1;
        n1.data = 4;
        Node<double> n2;
        n2.data = 5.22;
        n1.next = &n2;
        cout << ((Node<double>*)n1.next)->data << endl;
}

I just wonder what you would want to do with this. Why create a list from a whole bunch of data that has no connection whatsoever?
0
 
DexstarCommented:
Mikendher,
> I am trying to create a templated linked list in C++, so I wrote this code to
> test whether one templated node could link to another templated node of
> different template type, but it gave me the error shown in comments. Any suggestions?

With the template that you gave, each node will have to be of the same type.  If you want nodes that have different types, then I would use a union like this:

class Node
{
public:
   union
   {
      float nFloat;
      int   nInt;
   };

   Node* next;
};

For more on Unions, read this:
http://www.msi.umn.edu/sp/sp_manuals/cc++/doc/language/ref/rucldun.htm

You could also use void pointers, but that creates a whole bunch of type safety issues that you should avoid if at all possible.  If you're using Windows, you could also use the VARIANT datatype.  That's one datatype that can hold everything.

In short, what you're trying to do won't work the way you're trying to do it.  There isn't a really easy way to get what you want either.  I'm just throwing out ideas.

Hope that helps,
Dex*
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MikendherAuthor Commented:
I am creating it as a holder for objects in a 3d engine I am writing. That way I can hold all of my data types (spheres, cones, etc) in series, and display the series. And that way, a series will be allowed to contain other series.

I dont know how, that is why I am asking. My computer science professor says it is possible by using inheritance and virtuality, but I cant think of a way (I know what those are).

~Mike
0
 
n_fortynineCommented:
>>How would you determine of what type the node 'next' points is anyway?
Good question, jkr. I was wondering about that too.
0
 
MikendherAuthor Commented:
And the union method would not be extensible (because you need to explicitly type all data types)

I will try variant and void pointers, see if that works.

~Mike
0
 
MikendherAuthor Commented:
The type of the next node is determined when the node is added to the list (it isn't in my code, but thats how I want it to work)

~Mike
0
 
jkrCommented:
>>The type of the next node is determined when the node is added to the list

And, once it is in the list, how do you find out of what type each node is?
0
 
DexstarCommented:
Mikendher,

> I dont know how, that is why I am asking. My computer science professor says
> it is possible by using inheritance and virtuality, but I cant think of a way
> (I know what those are).

Well, for complex objects like in a 3D engine, that is definitely the way to go.  Here is a quick preview:

class CObjectBase
{
protected:
   CObjectBase( int nObjectType )
   {
      m_ObjectType = nObjectType;
   };

public:
   int GetObjectType() const { return m_ObjectType; };

private:
   int m_ObjectType;
};

class CSphere : public CObjectBase
{
public:
   CSphere() : CObjectBase(SPHERE_TYPE)
   {
      // Other Init Goes Here
   }

   // Other Stuff for Spheres goes here.
};
 
class CCone : public CObjectBase
{
public:
   CCone() : CObjectBase(CONE_TYPE)
   {
      // Other Init Goes Here
   }

   // Other Stuff for Cones goes here.
};

Once you have that, you can use your original list and template, but use CObjectBase as the type.
Then you should be able to add both CSphere and CCone to the list without any problems.
 

You thought your professor was wrong?  :)

Dex*
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now