Jump to content
  • Advertisement
Sign in to follow this  
JS Lemming

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

This topic is 4835 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by JS Lemming
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.

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

CM

Share this post


Link to post
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
declared
In 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 ambiguous
C:/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 this post


Link to post
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 this post


Link to post
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 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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!