Solved

Templated Linked List (Each element different type)

Posted on 2003-10-24
9
310 Views
Last Modified: 2010-04-01
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
Comment
Question by:Mikendher
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9616929
Well, what should I say, the compiler is right :o)

How would you determine of what type the node 'next' points is anyway?
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9616965
#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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9616988
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
 

Author Comment

by:Mikendher
ID: 9616989
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:n_fortynine
ID: 9616993
>>How would you determine of what type the node 'next' points is anyway?
Good question, jkr. I was wondering about that too.
0
 

Author Comment

by:Mikendher
ID: 9617010
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
 

Author Comment

by:Mikendher
ID: 9617016
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
 
LVL 86

Expert Comment

by:jkr
ID: 9617030
>>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
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 9617078
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now