|
-
March 21st, 2010, 10:05 PM
#1
Assert error with destructor
So currently I am toying around with classes in the setting of a game in order to learn them better. However, when I attempt to free some dynamic memory with a class deconstructor, I the execution error "_BLOCK_IS_VALID"
//MONSTER.H
#ifndef MONSTER
#define MONSTER
class Monster
{
private:
char* description;
int atk;
int def;
int agi;
int HP;
public:
const char* getDescription();
int getAtk();
int getDef();
int getAgi();
int getHP();
void changeHP(int);
Monster(const char*, int, int, int, int);
~Monster();
};
#endif
//MONSTER.CPP
#include <iostream>
#include <cstring>
#include "Monster.h"
using namespace std;
const char* Monster::getDescription()
{
return description;
}
int Monster::getAtk()
{
return atk;
}
int Monster::getDef()
{
return def;
}
int Monster::getAgi()
{
return agi;
}
int Monster::getHP()
{
return HP;
}
void Monster::changeHP(int change)
{
HP+=change;
}
Monster::Monster(const char* desc, int attack, int defense, int agility, int hitPoints)
{
description=new char [strlen(desc)+1];
strcpy(description, desc);
atk=attack;
def=defense;
agi=agility;
HP=hitPoints;
}
Monster::~Monster()
{
delete [] description;
}
//MAINFILE.CPP
#include <iostream>
#include <iomanip>
#include "Monster.h"
using namespace std;
void displayMonster(Monster);
int main()
{
Monster ghost("Ghost",1,1,1,1);
displayMonster(ghost);
cin.get();
return 0;
}
void displayMonster(Monster monsterType)
{
cout<<"Monster name: "<<monsterType.getDescription()<<endl;
cout<<"Monster atk : "<<monsterType.getAtk()<<endl;
cout<<"Monster def : "<<monsterType.getDef()<<endl;
cout<<"Monster agil: "<<monsterType.getAgi()<<endl;
cout<<"Monster HP : "<<monsterType.getHP()<<endl;
}
I'm sorry if that took up a ton of space, I wasn't sure exactly what could be contributing to the error.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|