Solved

Templated Linked List (Each element different type)

Posted on 2003-10-24
9
309 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

12 Experts available now in Live!

Get 1:1 Help Now