# [C++] Iterating through a Doubly Linked List... with Inheritance

## Recommended Posts

Hello peoples. Are any of you fammiliar with the data structures book called "Data Structures for Game Programmers" by Ron Penton? Well, I'm trying to get two aspects that I have learned from that book to work together. First let me show you the code I have so far. The class from which other classes are based:
class Object
{
public:
virtual void Update() = 0;
virtual void Draw() = 0;
virtual void Delete() = 0;
};
An example of one of the classes based on the one above (not finished though)
class Player : public Object
{
public:
float x,y,xvel,yvel;

float facing;

int frame;
int frame_counter;

Image character_set;

int life;
int score;
int invincible;

int coins_collected;
int enemies_killed;

int looking_up;

int going_pipe;
int going_door;

bool captain;

void Update()
{

}

void Draw()
{

}

void Delete()
{

}
};
Now, my goal is to be able to have a doubly linked list of all the things that I can iterate through and call Update() and such. Well after creating a global list of objects (pointers to them actually):
DLinkedList<Object*> Objects;
I attempted to iterate through them and call the update function:
		DListIterator<Object*> itr;
itr = Objects.GetIterator();
for(itr.Start(); itr.Valid(); itr.Forth())
{
itr.Update();
}
But when I compile I get this disgusting message: [quote="Microsoft Visual C++"] blaablaa.cpp: In function void DrawFrame()': blaablaa.cpp:165: no matching function for call to DListIterator<Object*>::Update()' NMAKE : fatal error U1077: 'C:\DevKitDC\bin\g++.exe' : return code '0x1' Stop. Error executing NMAKE. blaablaa.exe - 1 error(s), 4 warning(s)[/quote] I think I did something wrong in the inheritance department... And yes I am using "LinkedList.h". By the way, this is compiled for the DreamCast if that has anything to do with it (it doesn't...). Any suggestions?

##### Share on other sites
Basically it's telling you that DListIterator doesn't have an Update() function. I don't know what functions DListIterator does have though, so I can't suggest much more than to check precisely how to use it and what functions it has.

##### Share on other sites
Your iterator must have some method of dereferencing it (to get the current instance it refers to) before you can do anything else but we don't know what methods/operations your iterator type has.

I would also like to just point out the C++ standard library provides list type, once you've learnt data structures & algorithms learn to use the C++ standard library containers & algorithms and use that in real code.

##### Share on other sites
Okay, I'll do some research on this list you speak of. Thanks!

##### Share on other sites
Welp, I've adopted the use of the std's list. But I basicly got the same error as the first time. I guess my real question is how do I "dereference" my stuff.
		list<Object*>::iterator itr;		for(itr=Objects.begin(); itr!=Objects.end(); ++itr)		{			itr.Update();		}

I'm not sure if I explained this very well, but I'm tring to call the Update() function that is found in classes like Player in my first post... not in the Linked List structure.

##### Share on other sites
Use ( *itr )->Update() instead of itr.Update(). An iterator is kind of a glorified pointer in that it "points" to an object in your container. So you are, in effect, dereferencing the iterator, then dereferencing your pointer.

Forgive the blatent interchangeability of terms.

jfl.

##### Share on other sites
Quote:
 Original post by JS LemmingWelp, I've adopted the use of the std's list. But I basicly got the same error as the first time. I guess my real question is how do I "dereference" my stuff. list::iterator itr; for(itr=Objects.begin(); itr!=Objects.end(); ++itr) { itr.Update(); } I'm not sure if I explained this very well, but I'm tring to call the Update() function that is found in classes like Player in my first post... not in the Linked List structure.

You want (*itr)->Update().

CM

##### Share on other sites
Wow, you people are probably sick of hearing from me.. but I tried your solution and the compiler basicly barfed on me. Is this easily fixed???

In file included from /cygdrive/c/DevKitDC/include/g++-v3/bits/std_cwchar.h:39,                 from /cygdrive/c/DevKitDC/include/g++-v3/bits/fpos.h:40,                 from /cygdrive/c/DevKitDC/include/g++-v3/bits/std_iosfwd.h:41,                 from /cygdrive/c/DevKitDC/include/g++-v3/bits/stl_algobase.h:77,                 from /cygdrive/c/DevKitDC/include/g++-v3/bits/std_list.h:61,                 from /cygdrive/c/DevKitDC/include/g++-v3/list:31,                 from blaablaa.cpp:21:/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:55: clock_t' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:57: tm' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:59: clock' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:61: mktime' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:63: asctime' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:64: ctime' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:65: gmtime' not declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:66: localtime' not    declared/cygdrive/c/DevKitDC/include/g++-v3/bits/std_ctime.h:67: strftime' not    declaredIn file included from blaablaa.cpp:30:GameHeader.h: In function int Distance(int, int, int, int)':GameHeader.h:62: call of overloaded sqrt(int)' is ambiguousC:/DevKitDC/include/newlib-libm-sh4/math.h:59: candidates are: double    sqrt(double)/cygdrive/c/DevKitDC/include/g++-v3/bits/std_cmath.h:461:                 long    double std::sqrt(long double)/cygdrive/c/DevKitDC/include/g++-v3/bits/std_cmath.h:455:                 float    std::sqrt(float)NMAKE : fatal error U1077: 'C:\DevKitDC\bin\g++.exe' : return code '0x1'Stop.Error executing NMAKE.

At the top of my program I did:
#include <list>using namespace std;

Just like a reference page told me too. Is that causing the troubles?

##### Share on other sites
The first problem (whole bunch of stuff not defined) looks like it is the fault of the library - but it probably isn't - chances are it's something obscure in your code causing it. It's hard to tell what exactly.

The second error is easy to fix - cast your integer to double (you're getting the square root of an integer in the first place!?). The result would look something like:
double foo = sqrt((double)myint);

##### Share on other sites
Sigh. Can you give an example of what obscure code would look like? Do you mean like, I don't know, the order in which I included files or something? Because the rest of my code it just like normal c++. Here is basicly what the top of my program looks like:
#include <list>using namespace std;//this is needed by KOS#define _arch_dreamcast#include <kos.h>#include <stdlib.h>#include <cmath>#include <png/png.h>int Zindex;//Include game sources#include "Image.cpp"#include "GameHeader.h"#include "Input.cpp"#include "Player.h"extern uint8 romdisk[]; KOS_INIT_ROMDISK(romdisk);`

If that is not the problem.... then.. I don't know.

##### Share on other sites
try moving the "using namespace std;" statement to after those includes.
You might introduce a name clash the way you have it.
"int Zindex;" should probably be after the includes too.

It also strikes me as odd that you are including two cpp files. That's quite likely to be a mistake.

##### Share on other sites
Well about the 2 cpp files... In the end there will be about 8 cpp files. See when I was first learning C++, these programmers at some abandoned forum told me that it was just plain easier to include .cpp's then .h's. Now I wonder why I still do that, but I think that all it really does is just insert the code like it was always there. I guess that's a horrible practice right? Anyway I'll try what you said.

EDIT: I tried your suggestion but no dice. The exact same error occured. I'm begining to think that maybe the std is corrupt or something...

##### Share on other sites
Still can't figure out your problem with the includes. Removing "using namespace std" dosn't help? Interesting.

On the included C++ files. Generally this is a bad idea. At best, it means you can't do incremental linking (only need to compile the C++ files that change) and will make compilation much slower. At worst, it causes actual issues at the source code level (mismatched dependencies, multiple-inclusion, etc).

##### Share on other sites
I'd like to thank everyone who helped me out here. As it turns out, my std was actually broken as a fellow DC developer told me. So I pretty much can't do anything about it. I need a new version of KOS. Well, thanks again.

EDIT: Heh, sorry. But I just reallized I was saying "std" instead of "stl." Whoops!

[Edited by - JS Lemming on June 26, 2005 11:40:10 AM]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627734
• Total Posts
2978845

• 10
• 10
• 21
• 14
• 12