Advertisement

Problems with classes and linked lists

Started by April 27, 2000 06:54 AM
8 comments, last by Nanoprobe 24 years, 7 months ago
Hi there, I have this problem with the engine I''m developing. I have my entities structured so that as well as having basic parameters, each entity can have a list of variables attached to it, which are stored as two 32 byte strings (the name and the data). The problem I''m having is that when I try to compile this function to find a variable by giving it''s name (they''re stored in a linked-list), it says that it cannot be casted to the class it was based on. Now, I thought that if you had a pointer to a derived/base class, that pointer could point to anything derived from the base class. Oh, just look at the code. // class nodelink - the most basic class that links nodes together class nodelink { public: nodelink *prev; // Previous sibling node in the map nodelink *next; // Next sibling node in the map nodelink *parent; // This node''s parent node nodelink *firstChild; // This node''s first child node nodelink *lastChild; // This node''s last child node }; // class quasiVar - a universal variable class quasiVar : public nodelink { public: char ident[32]; // The name of the variable char data[32]; // The data stored in the variable }; // class entity - the base class for an object''s info class entity : public nodelink { public: matrix_4d matrix; nodelink *variables; // This entity''s variable list node }; // VariableFind - returns a pointer to a named variable quasiVar *VariableFind ( nodelink *owner, char *ident ) { quasiVar *temp; // Don''t start if there isn''t any variables if ( !owner->firstChild ) return NULL; temp = owner->firstChild; // Go through each variable until the right one is found do { if ( temp->ident == ident ) { return temp; } temp = temp->next; } while ( temp ); // We didn''t find it, return NULL return NULL; } - Nanoprobe

E-Mail

Colliderring.com

ICQ# 48856642

- Nanoprobe
The only thing I can think of is that you need a dynamic cast on the line:

temp = owner->firstChild;

Sorry, that is all I can think of... (and I know that dynamic cast is frowned upon by many...)

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
You actually can cast it if u have a interface class on the base which implements pure virtual functions.
e.g
class abc {
public:
void bcd()=0;
};

Why dont you use a void* instead of the base. And dont use a link list because it is slow to traverse. Use the STL Vector class. I used a Vector class in my engine to create an object factory that can return an object pointer just by using a string name.

Ummm, why even use a vector? You want to convert strings to variables? Use an STL map. You''re making so much more work for yourself by implementing this yourself. Pick the right data structure and save yourself a ton of headaches. Plus, it''ll be truly faster (from an algorithmic point of view) not just faster by some constant factor.

-Brian
you are declaring the classes totally wrong! you''re doing this:
class myClass {int info;};myClass stuff; 


This is wrong. In order for this to work, you would have to delclare the class like this:
typedef     class theClass{int info;} myClass;myClass stuff; 


in all, you need to do:
class   quasiVar *temp; 


...to declare it.
*sigh*


The_Minister
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive
Advertisement
Zipster: Why would you use typedef for that purpose? It would be easier to avoid naming collisions if you did typedef like this:

typedef class {
__unnamed(); // constructor
~__unnamed(); // destructor
} MyClass;

But why do it that way either? typedef just makes another name for the type. So, all you are really doing in your example is making a type named theClass and then making a synonym for it called myClass. You now have two symbols for every class, which just makes it confusing.


Nanoprobe: I''ve seen lots of people use strings to make things like that, from custom functions to custom variables, etc. There are FAR better ways to do all of that stuff, and better yet they''re a standard part of the language. There is no programming task that cannot be logically modeled in C++.

char arrays do not a language make...

If you''re trying to implement a collection class, why not use STL like osmanb said?


- null_pointer
Sabre Multimedia
Yeah,
And to clarify on Zipster''s post... You don''t need to do that. In C, structs were not in the same namespace as other types, so you had to declare them specially. C++ has no such rule, so class and struct names can be used without the typedef.

-Brian
Hey! Give me a break - I''m only 15.

I''ll check out those vector thingamies in a sec. I got my head around C++ a while ago (those For Dummies books really do come in handy! ), but I only got my head around vectors and matrices a bit back, so if these vector thingies are hard to understand...

Besides, the rate I''m going I''ll be finished in the year 2005, and then we''ll all have 4Ghz processors, so speed won''t be a problem.



- Nanoprobe

E-Mail

Colliderring.com

ICQ# 48856642

- Nanoprobe
HA! Fixed the problem. Just had to restructure the classes a tiny bit.

I looked at the Vector template and decided I wanted to stick with what I knew. Besides, my engine is going to be more about precision at the moment rather than speed.



- Nanoprobe

E-Mail

Colliderring.com

ICQ# 48856642

- Nanoprobe

This topic is closed to new replies.

Advertisement