Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Templated Linked List (Each element different type)

Posted on 2003-10-24
9
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 750 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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