Solved

Templated Linked List (Each element different type)

Posted on 2003-10-24
9
329 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 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 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