Thread: Visualisation Component & Sprite Class

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Visualisation Component & Sprite Class

    My sprite class:
    Code:
    //sprite.h file
    
    #include <HAPI_lib.h>
    
    class CSprite
    {
    public:
    	CSprite(void);
    	~CSprite(void);
    	bool initialise ( const char *);
    	
    private:
    	BYTE *m_data;
    	int m_width;
    	int m_height;
    
    };
    
    //sprite.cpp file
    
    #include ".\sprite.h"
    #include <assert.h>
    #include <HAPI_lib.h>
    
    CSprite::CSprite(void)
    {
    	*m_data = 0;
    	m_width = 0;
    	m_height = 0;
    }
    
    CSprite::~CSprite(void)
    {
    	if (*m_data = 0)
    		delete[] *m_data;
    }
    
    bool CSprite::initialise (const char *textureFilename)
    {
    	assert (textureFilename);
    
    if (HAPI_LoadTexture (textureFilename, &m_data, &m_width, &m_height))
    		return false;
    
    		return true;
    
    }
    1. 'delete' cannot delete objects that are not pointers


    __________________________________________________ _
    NOTES:

    - The program is written in C++, using Visual Studio 2003 as a Win32 project. The API is one designed by a university lecturer called HAPI.
    - BYTE* m_data is supposed to be a pointer to my texture, which is loaded via the HAPI_LoadTexture function.

    __________________________________________________ __

    So does anybody have any idea on how i should rid my program of these errors? i have a visualisation class as well which has functions to Create and Draw sprites to the screen.
    Last edited by mindofpoison; 12-12-2005 at 08:29 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    #include <windows.h>

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    cheers, corrected all those errors bar one.
    here's the next be-actch.

    Code:
    //visualisation class interface
    #pragma once
    
    #include <vector>
    
    //forward declaration of class CSprite
    class CSprite;
    
    //Visualisation class with constructor/destructor and functions to create and draw sprites
    class CVisualisation
    {
    public:
    	CVisualisation(void);
    	~CVisualisation(void);
    
    	bool CreateSprite (const char *, int *);
    	void DrawSprite (int, const);
    
    private:
    	std::vector <CSprite*> m_spriteVector;
    
    };
    
    //visualisation class implementation
    //include header files for visualisation class and sprite class
    #include "visualisation.h"
    #include "sprite.h"
    #include <assert.h>
    
    CVisualisation::CVisualisation(void)
    {
    }
    
    CVisualisation::~CVisualisation(void)
    {
    	for (size_t i=0; i < m_spriteVector.size(); i++)
    		delete m_spriteVector [i];
    }
    
    bool CVisualisation::CreateSprite (const char *textureFilename, int *id)
    {
    	assert (textureFilename);
    	assert ( id );
    
    	//create new instance of sprite and then add it to the vector
    	CSprite *newSprite = new CSprite;
    
    	if (!newSprite -> initialise (textureFilename))
    	{
    		delete newSprite;
    		return false;
    	}
    
    	m_spriteVector.push_back (newSprite);
    	*id = m_spriteVector.size() - 1;
    	return true;
    	}
    
    
    void CVisualisation::DrawSprite (int id, const vector2 &pos)
    {
    	assert (id >= 0 && id <= m_spriteVector.size());
    
    	m_spriteVector[id] -> DrawSprite (&pos);
    }
    Problems
    [1] Visualisation.cpp(31): warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    [2] Visualisation.cpp(36): error C2143: syntax error : missing ',' before '&'
    [3] Visualisation.cpp(38): warning C4018: '<=' : signed/unsigned mismatch
    [4] Visualisation.cpp(40): error C2065: 'pos' : undeclared identifier

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    for (size_t i=0; i < m_spriteVector.size(); i++)
    delete m_spriteVector [i];
    Either specify i as an integer or use an iterator.

  5. #5

    Join Date
    May 2005
    Posts
    1,042
    Using the .erase() method deletes the raw pointers only (not actually calling destructors of the object). I use std::for_each to delete items, from memory, in a std::vector

    Code:
    #include	<algorithm>	//std::for_each
    
    template<class T>
    struct VectorDeleter
    {    	
    	void operator()(T * & obj) 	//reference to a pointer, not a pointer to a reference 
    	{    		
    			delete obj;    			
    			obj = NULL;	//I don't know if this actually does anything...hmm	
    	}
    };
    
    
    #define	DELETE_MEMORY_AND_ERASE(a,t)	std::for_each(a.begin(),a.end(),VectorDeleter<t>());	a.clear();
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM