C++ 'delete' keyword HELP NEEDED!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

C++ 'delete' keyword HELP NEEDED!

Post by VoidElite »

OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.

NOTE: this is for my show and hide system for sprites.

Here is the code for the Sprite class:

Code: Select all

#include "Content.h"

using namespace std;

#ifndef SPRITE
#define SPRITE
class Sprite{
      public:
             Sprite();
             Sprite(string file2,int x2,int y2,bool visible2);
			 void show();
			 void hide();
			 SDL_Surface *bmp;
			 string file;
			 int x;
			 int y;
			 bool visible;
};
Sprite::Sprite(){
	file="null.bmp";
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=0;
	y=0;
	visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
	file=file2;
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=x2;
	y=y2;
	visible=visible2;
}
void show(){
	SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
#endif
You should pay attention to the Sprite::hide() function:

Code: Select all

void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).

Wait!

I have another few errors. Now that I've fixed that I'm getting 'screen' has not been declared in this scope but 'screen' is declared in 'Content.h' which is being included into 'Sprite.h'.

Anywhoo here's 'Content.h':

Code: Select all

#ifndef CONTENT
#define CONTENT
#include <iostream>
#include "SDL\SDL.h"
#include "windows.h"
#include "Environment.h"
#include "Sprite.h"
#include "Tile.h"
#include "Map.h"
#include "World.h"
#include "Object.h"
#include "Creature.h"
#include "Npc.h"
#include "Player.h"

using namespace std;

int mapIndex=0;

SDL_Surface *screen=NULL; //SDL's screen
Sprite *sprite=new Sprite("dude.bmp",10,10,true); //test sprite
Player *player=new Player(); //Main Player
World *world=new World(); //the world
#endif
Help once again would be greatly appreciated. :)
Last edited by VoidElite on Tue Apr 26, 2011 10:49 am, edited 1 time in total.
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
Rapid Cube
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Mon Mar 14, 2011 11:43 pm
Programming Language of Choice: C++

Re: C++ 'delete' keyword HELP NEEDED!

Post by Rapid Cube »

VoidElite wrote:OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.

NOTE: this is for my show and hide system for sprites.

Here is the code for the Sprite class:

Code: Select all

#include "Content.h"

using namespace std;

#ifndef SPRITE
#define SPRITE
class Sprite{
      public:
             Sprite();
             Sprite(string file2,int x2,int y2,bool visible2);
			 void show();
			 void hide();
			 SDL_Surface *bmp;
			 string file;
			 int x;
			 int y;
			 bool visible;
};
Sprite::Sprite(){
	file="null.bmp";
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=0;
	y=0;
	visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
	file=file2;
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=x2;
	y=y2;
	visible=visible2;
}
void show(){
	SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
#endif
You should pay attention to the Sprite::hide() function:

Code: Select all

void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).

Help would be greatly appreciated. :)
you implemented the show and hide functions wrong
this is how you need to do it

Code: Select all

void Sprite::show()
{
        SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void Sprite::hide()
{
        SDL_FreeSurface(bmp);
	delete this; 
}
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: C++ 'delete' keyword HELP NEEDED!

Post by VoidElite »

Rapid Cube wrote:
VoidElite wrote:OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.

NOTE: this is for my show and hide system for sprites.

Here is the code for the Sprite class:

Code: Select all

#include "Content.h"

using namespace std;

#ifndef SPRITE
#define SPRITE
class Sprite{
      public:
             Sprite();
             Sprite(string file2,int x2,int y2,bool visible2);
			 void show();
			 void hide();
			 SDL_Surface *bmp;
			 string file;
			 int x;
			 int y;
			 bool visible;
};
Sprite::Sprite(){
	file="null.bmp";
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=0;
	y=0;
	visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
	file=file2;
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=x2;
	y=y2;
	visible=visible2;
}
void show(){
	SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
#endif
You should pay attention to the Sprite::hide() function:

Code: Select all

void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).

Help would be greatly appreciated. :)
you implemented the show and hide functions wrong
this is how you need to do it

Code: Select all

void Sprite::show()
{
        SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void Sprite::hide()
{
        SDL_FreeSurface(bmp);
	delete this; 
}
My bad! Thanks. :)
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: C++ 'delete' keyword HELP NEEDED!

Post by VoidElite »

Rapid Cube wrote:
VoidElite wrote:OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.

NOTE: this is for my show and hide system for sprites.

Here is the code for the Sprite class:

Code: Select all

#include "Content.h"

using namespace std;

#ifndef SPRITE
#define SPRITE
class Sprite{
      public:
             Sprite();
             Sprite(string file2,int x2,int y2,bool visible2);
			 void show();
			 void hide();
			 SDL_Surface *bmp;
			 string file;
			 int x;
			 int y;
			 bool visible;
};
Sprite::Sprite(){
	file="null.bmp";
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=0;
	y=0;
	visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
	file=file2;
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=x2;
	y=y2;
	visible=visible2;
}
void show(){
	SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
#endif
You should pay attention to the Sprite::hide() function:

Code: Select all

void hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).

Help would be greatly appreciated. :)
you implemented the show and hide functions wrong
this is how you need to do it

Code: Select all

void Sprite::show()
{
        SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void Sprite::hide()
{
        SDL_FreeSurface(bmp);
	delete this; 
}
Wait!

I have another few errors. Now that I've fixed that I'm getting 'screen' has not been declared in this scope but 'screen' is declared in 'Content.h' which is being included into 'Sprite.h'.

Anywhoo here's 'Content.h':

Code: Select all

#ifndef CONTENT
#define CONTENT
#include <iostream>
#include "SDL\SDL.h"
#include "windows.h"
#include "Environment.h"
#include "Sprite.h"
#include "Tile.h"
#include "Map.h"
#include "World.h"
#include "Object.h"
#include "Creature.h"
#include "Npc.h"
#include "Player.h"

using namespace std;

int mapIndex=0;

SDL_Surface *screen=NULL; //SDL's screen
Sprite *sprite=new Sprite("dude.bmp",10,10,true); //test sprite
Player *player=new Player(); //Main Player
World *world=new World(); //the world
#endif
Help once again would be greatly appreciated. :)
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: C++ 'delete' keyword HELP NEEDED!

Post by adikid89 »

Don't ever delete this. You're just asking for crashes...
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: C++ 'delete' keyword HELP NEEDED!

Post by LeonBlade »

Yeah... deleting this isn't really the safest thing to do ;)

Create your draw function passing in a "draw to..." SDL_surface pointer that way you can just reference the pointer to draw to.
I think this is what you're asking...

I never liked how SDL did things this way having to blit to surfaces, that's why I use OpenGL now.
There's no place like ~/
User avatar
BlobOfFailure
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Sat Nov 27, 2010 10:38 am
Programming Language of Choice: C++

Re: C++ 'delete' keyword HELP NEEDED!

Post by BlobOfFailure »

The second problem you had, the one you made a thread for, I answered in its thread yesterday, not sure if you saw it but it should help with that problem.
Post Reply