Jump to content
  • Advertisement
Sign in to follow this  
lordimmortal2

Class Template Error

This topic is 2587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just tried to replace some classes and functions with templates to reduce some code substantially and seeing as how it's my first time using templates and inheritance, there's some confusing errors. I'm using C++.

I've worked it into getting errors such as this:
error C2440: '=' : cannot convert from 'cLinkedList<T> *const ' to 'cEntity *'
1> with
1> [
1> T=cEntity
1> ]
1> Cast from base to derived requires dynamic_cast or static_cast
1> c:\users\owner\documents\visual studio 2010\projects\rpg\rpg\clinkedlist.cpp(9) : while compiling class template member function 'int cLinkedList<T>::FindEmptyID(void)'
1> with
1> [
1> T=cEntity
1> ]


And I'm quite confused about how to fix this.

Relevant code:

cLinkedList.h:
#pragma once

template <class T>
class cLinkedList
{
public:
T *next;

int FindEmptyID();

T * ScanListPrevious(int ID);
T * ScanList(int ID);
T * ScanListEnd(); // Returns the last entity in the list
};


cLinkedList.cpp:
#include "cLinkedList.h"

#include "cTile.h"
#include "cEntity.h"

template <class T>
int cLinkedList <T>::FindEmptyID()
{
T *dummy, *dummyNext;

if(this != 0)
{
dummy = this;

while(true)
{
dummyNext = dummy->next;

if(dummyNext == 0)
{
// Impossible to dicern the next ID, assume that the final one in the list is the last ID
return dummy->ID + 1;
}
if(dummyNext->ID - dummy->ID > 1)
// This checks if there is a break between ID numbers and returns ID + 1 which fills the hole
return dummy->ID + 1;

dummy = dummy->next;
}
}
else
{
// Error, no root
}
}

template <class T>
T * cLinkedList <T>::ScanListPrevious(int ID)
{
T *previous; // Dummy struct that contains the previous node
T *listTraverse; // Dummy struct to check if there is a next node as we traverse

// Start at the beginning of the list
listTraverse = this;

if(listTraverse != 0)
{
while(listTraverse->next != 0)
{
previous = listTraverse;

listTraverse = listTraverse->next;

if(listTraverse->ID == ID)
return previous;
}
}
else
{
// Error, no root
}
}

template <class T>
T * cLinkedList <T>::ScanList(int ID)
{
// Dummy struct to check if there is a next node as we traverse
T *listTraverse;

// Start at the beginning of the list
listTraverse = this;

if(listTraverse != 0)
{
while(listTraverse->next != 0)
{
if(listTraverse->ID == ID)
return listTraverse;

listTraverse = listTraverse->next;
}
}
else
{
// Error, no root
}
}

template <class T>
T * cLinkedList <T>::ScanListEnd()
{
T *dummy;

if(this != 0)
{
dummy = this;

while(dummy->next != 0)
dummy = dummy->next;
}
else
{
// Error, no root
}

return dummy;
}

template class cLinkedList<cTile>;
template class cLinkedList<cEntity>;
template class cLinkedList<cLayer>;


Edit: Gah, I always forget to leave out a crucial bit of information. cLayer, cTile, and cEntity all inherit cLinkedList. They all call the functions inherited by this->ScanList(ID), or equivalent.

So basically, what am I doing wrong? As always, all replies and help is very appreciated.

Share this post


Link to post
Share on other sites
Advertisement
This doesn't look rightdummy = this;this is a cLinkedList<T>* and dummy is T* so that will not work.

Also when working with templates you have to define all the functions in the header file.

Share this post


Link to post
Share on other sites
Thanks for your reply =D.


This doesn't look rightdummy = this;this is a cLinkedList<T>* and dummy is T* so that will not work.


Then what should I change this/anything to?


Also when working with templates you have to define all the functions in the header file.
[/quote]

I was following this article: http://www.cplusplus...articles/14272/ and as you can see in my code, I've tried using explicit instantiation. Implicit would be the better way to do it, but I don't have many types for the template to be used right now and it produces the same errors...

Share this post


Link to post
Share on other sites
Oh I didn't see the explicit instantiation. I don't know much about it so I can't say anything more about that.

What you should change depends on what are trying to do. What is it you want dummy to point to?

Checking if this is null is a bit strange. Do you want to be able to call the member functions on a null pointer or something? Normally you just assume the this pointer is not null.

Share this post


Link to post
Share on other sites

Oh I didn't see the explicit instantiation. I don't know much about it so I can't say anything more about that.

What you should change depends on what are trying to do. What is it you want dummy to point to?

Checking if this is null is a bit strange. Do you want to be able to call the member functions on a null pointer or something? Normally you just assume the this pointer is not null.


Well the reason I'm using the template is because cTile, cLayer, and cEntity all use linked lists for their data management and all their basic functions are exactly the same except for data types (ScanList and such). Dummy is just a pointer of type cTile/cLayer/cEntity that is used as a placeholder to check certain values as you traverse down the list.

If the pointer was null, I'd have errors so I check if the root of the linked list actually exists to avoid potential errors (would probably only occur from the programmer but meh).

Edit: Gah, I always forget to leave out a crucial bit of information. cLayer, cTile, and cEntity all inherit cLinkedList. They all call the functions inherited by this->ScanList(ID), or equivalent.

Share this post


Link to post
Share on other sites
It is a very unusual design to have all the objects extend linked list here. Is there a reason it must be an intrusive list, or can you not have have the objects in an external std::list<> and use a standard predicate search for finding nodes by ID?

Share this post


Link to post
Share on other sites

It is a very unusual design to have all the objects extend linked list here. Is there a reason it must be an intrusive list, or can you not have have the objects in an external std::list<> and use a standard predicate search for finding nodes by ID?


I'm confused. Could you please elaborate? I built this entire thing to learn how to use linked lists. I'm self-learned in programming so when it comes to standard usage of things, then I have no idea.

Share this post


Link to post
Share on other sites

I'm confused. Could you please elaborate? I built this entire thing to learn how to use linked lists. I'm self-learned in programming so when it comes to standard usage of things, then I have no idea.


Just quickly glancing it, it looks like you are trying to force your linked list to act like an array. Check out the STL version of list to see functions that might be good http://www.cplusplus.com/reference/stl/list/

Share this post


Link to post
Share on other sites

[quote name='lordimmortal2' timestamp='1320959241' post='4882732']
I'm confused. Could you please elaborate? I built this entire thing to learn how to use linked lists. I'm self-learned in programming so when it comes to standard usage of things, then I have no idea.


Just quickly glancing it, it looks like you are trying to force your linked list to act like an array. Check out the STL version of list to see functions that might be good http://www.cplusplus...rence/stl/list/
[/quote]

A linked list acts like an array in that it's a collection of a bunch of data values of a certain data type. This is how it should work, isn't it? I'm confused about how I'm forcing it to work like an array and how that would produce the template error I'm getting.

Share this post


Link to post
Share on other sites
cLayer, cTile, and cEntity all inherit cLinkedList.

dummy = this; You are here trying to assign a base class pointer to a derived class pointer. This cast is not implicit, for good reason.

I think you should reconsider your design.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!