Sign in to follow this  


Recommended Posts

Well, here's my end goal. If someone has a better way to go about it, let me know. I'm writing a 2D DirectX game in C++ and am drawing sprites to the screen. I have all the sprites wrapped in a class called "spriteInfo" and they're being stuffed into a std::list<spriteInfo>, which throws everything on the screen using the spriteInfo structs. The game is 1-4 players, so I have another 1-4 sprites (the character(s)) that I want the user(s) to control. These sprites are wrapped in a class called "character", which inherits from "spriteInfo". They're being added to the spriteInfo list, so they're drawn to the screen like everything else. I need to be able to quickly access the data members of each character so I can update their positions in the world. So here's what I'm doing... I've got an array of iterator pointers (std::list<character>::iterator*), that theoretically should be pointing to the location of the iterator in the list where the character sprite(s) are being stored. However, I'm getting a compiler error when I try this. It looks like it's related to inheritance. Here's what the code looks like:
class spriteInfo {
	float x, y, elevation, scale;
	float width, height;
	float distToCamera;
	IDirect3DTexture9* texture;
class character : public spriteInfo {
	float angle;
	float angleAccel;
	float speed;

std::list<spriteInfo> spriteList;
characters = new std::list<character>::iterator*[cameraCount];
for (int i=0; i<cameraCount; i++) {
	character c;
	// character data populated here
	characters[i] = &spriteList.begin();

Here is the error I am getting:
error C2440: '=' : cannot convert from 'std::list<_Ty>::iterator *__w64 ' to 'std::list<_Ty>::iterator *' with [ _Ty=spriteInfo ] and [ _Ty=character ] Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
How would I go about getting this fixed?

Share this post

Link to post
Share on other sites
character[i] is of type std::list<character>::iterator*
&spriteList.begin() is of type std::list<spriteInfo>::iterator*
The first is a pointer to an iterator of a list containing characters, but begin returns an iterator to a list containing sprite infos, so one iterator points to one kind of data and the other points to another kind of data. What you most likely want is dynamic polymorphism, to achieve this in C++ you need pointers. So spriteList should be a list of spriteInfo* and characters should be an array of pointers to iterators to a std::list containing spriteInfo* (yes you should use spriteInfo*, not character*).

Share this post

Link to post
Share on other sites
Okay, I've converted it over and it's still not working. I've got this...

std::list<spriteInfo*>::iterator* base = characters[i];
character* c = **base;

It's giving me this error:

error C2440: 'initializing' : cannot convert from 'std::allocator<_Ty>::value_type' to 'character *'
_Ty=spriteInfo *
Cast from base to derived requires dynamic_cast or static_cast

I've never used dynamic_cast/static_cast before, and I wasn't able to get it working. Any more ideas? It's getting closer.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this