Public Group

# Dynamic Memory Allocation - help needed

This topic is 4121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i am currently developing a 2d rpg and have ran across a problem that i can't identify in my inventory system its not a compile error as far as i'm aware i am trying to allocate some memory for an array of a custom class with a pointer pointing to it. an example of what i'm doing class wise:


class foo{
private:
int m_DynamicNumber;

int *p_CustomClassCount;
CustomClass *p_CustomClass;

public:
class();
}

//in constructor

class::class(){
m_DynamicNumber = 5;

p_CustomClass = new CustomClass[m_DynamicNumber];
p_CustomClassCount = new CustomClassCount[m_DynamicNumber];

}


now that is basically what i'm doing (full source code included at the end) i think it might be that i'm trying to allocate the memory in the constructor but i dont see the problem with that. it is compiling and when i run the program it does allocate memory but only for 1 not for the entire array,i've also checked if i can access the other members (as i thought it just might not be showing up in msvc) but i cant. any help would be appreciated thank you (note: the problem i'm talking about is in the Inventory code,the item code is there because it is a header in inventory) Item.h
#pragma once
#include <windows.h>
#include <string>
#include <gl/gl.h>
#include "TGA.h"

using namespace std;

class Item{
protected:
string m_Name;
string m_ID;
string m_Description;
GLuint m_DropSprite;
GLuint m_InventorySprite;
public:
Item();
Item(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven);
string GetName();
string GetDesc();
string GetID();
GLuint GetDropSprite();
GLuint GetInventorySprite();

void SetName(string Name);
void SetDesc(string Desc);
void SetID(string ID);
void SetDropSprite(TGAImg *DropSprite);
void SetInventorySprite(TGAImg *InvenSprite);
void SetDropSprite(GLuint DropSprite);
void SetInventorySprite(GLuint InvenSprite);
};
class Item_Potion : public Item{
private:
int m_RestoreValue;
public:
Item_Potion();
Item_Potion(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,int Value);
int GetRestoreValue();
void SetRestoreValue(int Value);
};

class Item_MonsterDrop : public Item{
private:
int m_Level;
public:
Item_MonsterDrop();
Item_MonsterDrop(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,int Level);
int GetLevel();
void SetLevel(int Level);

};

class Item_Base : public Item{
private:
int m_Type;	// 0 for damage 1 for defence 2 for elemental
int m_Level;	//level's 1 through 5, 1 being the lowest
public:
Item_Base();
Item_Base(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,int Type,int Level);
int GetType();
int GetLevel();

void SetType(int Type);
void SetLevel(int Level);
};

private:
int m_Type; // 0 for Damage 1 for Defence 2 for elemental
int m_Level;	//level's 1 through 5, 1 being the lowest
int m_Value;	//Value of attack,defense or elemental addition
public:
Item_Upgrade(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,int Type,int Level);
int GetType();
int GetLevel();
int GetValue();

void SetType(int Type);
void SetLevel(int Level);
void SetValue(int Value);
};

class Item_Weapon : public Item{
private:
int m_Atk;
int m_Element;
int m_Percentage;
TGAImg m_AttackImg;
public:
Item_Weapon();
Item_Weapon(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,TGAImg AttackImg,int Atk,int Element,int Percentage);
int GetAtk();
int GetElement();
int GetPercentage();

void SetAtk(int Atk);
void SetElement(int Element);
void SetPercentage(int Percentage);
};

class Item_Armour : public Item{
private:
int m_Def;
int m_Element;
int m_Percentage;
public:
Item_Armour();
Item_Armour(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,int Def,int Element,int Percentage);
int GetDef();
int GetElement();
int GetPercentage();

void SetDef(int Def);
void SetElement(int Element);
void SetPercentage(int Percentage);
};


Item.cpp
#pragma once

#include "Item.h"

//Base Class Functions
Item::Item(){}		// default constructor DO NOT USE
Item::Item(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
}

string Item::GetDesc(){
return m_Description;
}
string Item::GetName(){
return m_Name;
}

string Item::GetID(){
return m_ID;
}
GLuint Item::GetDropSprite(){
return m_DropSprite;
}
GLuint Item::GetInventorySprite(){
return m_InventorySprite;
}

void Item::SetName(string Name){
m_Name = Name;
}
void Item::SetDesc(string Desc){
m_Description = Desc;
}
void Item::SetID(string ID){
m_ID = ID;
}
void Item::SetDropSprite(TGAImg *DropSprite){
glGenTextures(1,&m_DropSprite);
glBindTexture(GL_TEXTURE_2D,m_DropSprite);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, DropSprite->GetWidth(),DropSprite->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,DropSprite->GetImg());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
void Item::SetInventorySprite(TGAImg *InvenSprite){
glGenTextures(1,&m_InventorySprite);
glBindTexture(GL_TEXTURE_2D,m_InventorySprite);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, InvenSprite->GetWidth(),InvenSprite->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,InvenSprite->GetImg());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
void Item::SetDropSprite(GLuint DropSprite){
m_DropSprite = DropSprite;
}
void Item::SetInventorySprite(GLuint InvenSprite){
m_InventorySprite = InvenSprite;
}

//Potion Specific Functions
Item_Potion::Item_Potion(){}		// default constructor DO NOT USE
Item_Potion::Item_Potion(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,int Value){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
m_RestoreValue = Value;
}
int Item_Potion::GetRestoreValue(){
return m_RestoreValue;
}

void Item_Potion::SetRestoreValue(int Value){
m_RestoreValue = Value;
}

// Monster Drop Specific functions
Item_MonsterDrop::Item_MonsterDrop(){}	// default constructor DO NOT USE
Item_MonsterDrop::Item_MonsterDrop(string Name, string Description, string ID, TGAImg Drop, TGAImg Inven, int Level){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
m_Level = Level;
}

int Item_MonsterDrop::GetLevel(){
return m_Level;
}

void Item_MonsterDrop::SetLevel(int Level){
m_Level = Level;
}
// Synth Base Specific Functions
Item_Base::Item_Base(){}		// default constructor DO NOT USE
Item_Base::Item_Base(string Name, string Description, string ID, TGAImg Drop, TGAImg Inven, int Type, int Level){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
m_Type = Type;
m_Level = Level;
}

int Item_Base::GetType(){
return m_Type;
}

int Item_Base::GetLevel(){
return m_Level;
}

void Item_Base::SetLevel(int Level){
m_Level = Level;
}
void Item_Base::SetType(int Type){
m_Type = Type;
}

Item_Upgrade::Item_Upgrade(string Name, string Description, string ID, TGAImg Drop, TGAImg Inven, int Type, int Level){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
m_Type = Type;
m_Level = Level;
}

return m_Type;
}

return m_Level;
}

return m_Value;
}
m_Level = Level;
}
m_Type = Type;
}

m_Value = Value;
}
// Weapon Specific Functions
Item_Weapon::Item_Weapon(){}		// default constructor DO NOT USE
Item_Weapon::Item_Weapon(string Name,string Description,string ID,TGAImg Drop,TGAImg Inven,TGAImg AttackImg,int Atk,int Element,int Percentage){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
m_AttackImg = AttackImg;
m_Atk = Atk;
m_Element = Element;
m_Percentage = Percentage;
}

int Item_Weapon::GetAtk(){
return m_Atk;
}

int Item_Weapon::GetElement(){
return m_Element;
}
int Item_Weapon::GetPercentage(){
return m_Percentage;
}

void Item_Weapon::SetAtk(int Atk){
m_Atk = Atk;
}
void Item_Weapon::SetElement(int Element){
m_Element = Element;
}
void Item_Weapon::SetPercentage(int Percentage){
m_Percentage = Percentage;
}

//Armour Specific Functions
Item_Armour::Item_Armour(){}		// default constructor DO NOT USE
Item_Armour::Item_Armour(string Name, string Description, string ID, TGAImg Drop, TGAImg Inven, int Def, int Element, int Percentage){
m_Name = Name;
m_Description = Description;
m_ID = ID;
SetDropSprite(&Drop);
SetInventorySprite(&Inven);
m_Def = Def;
m_Element = Element;
m_Percentage = Percentage;
}

int Item_Armour::GetDef(){
return m_Def;
}

int Item_Armour::GetElement(){
return m_Element;
}
int Item_Armour::GetPercentage(){
return m_Percentage;
}

void Item_Armour::SetDef(int Def){
m_Def = Def;
}
void Item_Armour::SetElement(int Element){
m_Element = Element;
}
void Item_Armour::SetPercentage(int Percentage){
m_Percentage = Percentage;
}


Inventory.h
#pragma once
#include "Item.h"
class Inventory{
private:
int m_PotionSlots;
int m_DropSlots;
int m_BaseSlots;
int m_WeaponSlots;
int m_ArmourSlots;

int *m_PotionCount;
int *m_DropCount;
int *m_BaseCount;
int *m_WeaponCount;
int *m_ArmourCount;
public:
Item_Potion *m_PotionBag;
Item_MonsterDrop *m_DropBag;
Item_Base *m_BaseBag;
Item_Weapon *m_WeaponBag;
Item_Armour *m_ArmourBag;

Inventory();
Inventory(int PotionSlots,int DropSlots,int BaseSlots,int UpgradeSlots,int WeaponSlots,int ArmourSlots);
~Inventory();

void DestroyItem(int Slot);
void MoveItem(int RootSlot, int TargetSlot);

};


Inventory.cpp
#include "Inventory.h"

#define POTION_STACK_LIMIT 10
#define DROP_STACK_LIMIT 20
#define BASE_STACK_LIMIT 20
#define WEAPON_STACK_LIMIT 1
#define ARMOUR_STACK_LIMIT 1

Inventory::Inventory(){
m_PotionSlots = 5;		// sets up how many slots for each item type
m_DropSlots = 10;
m_BaseSlots = 10;
m_WeaponSlots = 4;
m_ArmourSlots = 4;

m_PotionBag = new Item_Potion[m_PotionSlots];	// assigns an array with amount of slots defined previously
m_PotionCount = new int[m_PotionSlots];			// and also creates an array that holds a counter for each slot
m_DropBag = new Item_MonsterDrop[m_DropSlots];	// so items can stack
m_DropCount = new int[m_DropSlots];
m_BaseBag = new Item_Base[m_BaseSlots];
m_BaseCount = new int[m_BaseSlots];
m_WeaponBag = new Item_Weapon[m_WeaponSlots];
m_WeaponCount = new int[m_WeaponSlots];
m_ArmourBag = new Item_Armour[m_ArmourSlots];
m_ArmourCount = new int[m_ArmourSlots];

for(int i = 0;i<m_PotionSlots;i++){	//Set all counters to 0
m_PotionCount = 0;			//could possibly do with memset but not sure
}									//if it sets it to 0 or the unsigned char version of 0
for(int i = 0;i<m_DropSlots;i++){
m_DropCount = 0;
}
for(int i = 0;i<m_BaseSlots;i++){
m_BaseCount = 0;
}
}
for(int i = 0;i<m_WeaponSlots;i++){
m_WeaponCount = 0;
}
for(int i = 0;i<m_ArmourSlots;i++){
m_ArmourCount = 0;
}

}
Inventory::Inventory(int PotionSlots,int DropSlots,int BaseSlots,int UpgradeSlots,int WeaponSlots,int ArmourSlots){
m_PotionSlots = PotionSlots;
m_DropSlots = DropSlots;
m_BaseSlots = BaseSlots;
m_WeaponSlots = WeaponSlots;
m_ArmourSlots = ArmourSlots;

m_PotionBag = new Item_Potion[m_PotionSlots];	// assigns an array with amount of slots defined previously
m_PotionCount = new int[m_PotionSlots];			// and also creates an array that holds a counter for each slot
m_DropBag = new Item_MonsterDrop[m_DropSlots];	// so items can stack
m_DropCount = new int[m_DropSlots];
m_BaseBag = new Item_Base[m_BaseSlots];
m_BaseCount = new int[m_BaseSlots];
m_WeaponBag = new Item_Weapon[m_WeaponSlots];
m_WeaponCount = new int[m_WeaponSlots];
m_ArmourBag = new Item_Armour[m_ArmourSlots];
m_ArmourCount = new int[m_ArmourSlots];

for(int i = 0;i<m_PotionSlots;i++){	//Set all counters to 0
m_PotionCount = 0;			//could possibly do with memset but not sure
}									//if it sets it to 0 or the unsigned char version of 0
for(int i = 0;i<m_DropSlots;i++){
m_DropCount = 0;
}
for(int i = 0;i<m_BaseSlots;i++){
m_BaseCount = 0;
}
}
for(int i = 0;i<m_WeaponSlots;i++){
m_WeaponCount = 0;
}
for(int i = 0;i<m_ArmourSlots;i++){
m_ArmourCount = 0;
}
}

Inventory::~Inventory(){
delete []m_PotionBag;
delete []m_PotionCount;
delete []m_DropBag;
delete []m_DropCount;
delete []m_BaseBag;
delete []m_BaseCount;
delete []m_WeaponBag;
delete []m_WeaponCount;
delete []m_ArmourBag;
delete []m_ArmourCount;
}

bool Item_Present = false;		//if item is already in the bag
bool Item_Fits = false;			//if the item is already in the bag and will fit into a stack
bool Empty_Space = false;
int Situation = 4;				// a value not in the enumeration(and thus not in the switch-case)
// enum types
// P_SIS  the item is present and a is has space in its stack
// NP_ES item is not already present and there is an empty space where it can go
// ALL_FULL there is no space for the item/ if it is present, all stacks are full
//
enum { P_SIS,NP_ES,ALL_FULL};

for(int i = 0;i < m_PotionSlots;i++){				//checks to see if item is already present in the bag
if(m_PotionBag.GetID() == Item.GetID()){
Item_Present = true;
}
}
for(int i = 0;i < m_PotionSlots;i++){				//checks to see if there are any empty spaces
if(m_PotionCount == 0){
Empty_Space = true;
}
}

if(Item_Present){									//if item is present then see if a stack has space
for(int i = 0;i < m_PotionSlots;i++){
if(m_PotionBag.GetID() == Item.GetID() && m_PotionCount < POTION_STACK_LIMIT){	// checks to see if the stacks of the items are full
Item_Fits = true;
}
}
}

if(Item_Present && Item_Fits){					//if there is a space in a stack of the item already
Situation = P_SIS;
}else if(Empty_Space){							//if item is not already there or item is there but full then check for empty spaces
Situation = NP_ES;
}else{											// otherwise item does not fit in bag
Situation = ALL_FULL;
}

switch(Situation){
case P_SIS:
for(int i = 0;i < m_PotionSlots;i++){
if(m_PotionBag.GetID() == Item.GetID()){
m_PotionCount ++;
return true;
break;
}
}
break;
case NP_ES:
for(int i = 0;i< m_PotionSlots;i++){
if(m_PotionCount == 0){					// if the block is empty
m_PotionBag.SetDesc(Item.GetDesc());			//then copy the item into the space
m_PotionBag.SetDropSprite(Item.GetDropSprite());
m_PotionBag.SetID(Item.GetID());
m_PotionBag.SetInventorySprite(Item.GetInventorySprite());
m_PotionBag.SetName(Item.GetName());
m_PotionBag.SetRestoreValue(Item.GetRestoreValue());
m_PotionCount ++;							// and add one to the slots counter
return true;
break;
}
}
break;
case ALL_FULL:
// INSERT ERROR MESSAGE HERE
return false;
break;
default:
// INSERT UNEXPECTED ERROR MESSAGE HERE
return false;
break;
}
return false;

}

if(m_PotionBag[Slot].GetID() == Item.GetID() && m_PotionCount[Slot] < POTION_STACK_LIMIT){
m_PotionCount[Slot] ++;
return true;
}
else if(m_PotionCount[Slot] == 0){
m_PotionBag[Slot].SetDesc(Item.GetDesc());			//then copy the item into the space
m_PotionBag[Slot].SetDropSprite(Item.GetDropSprite());
m_PotionBag[Slot].SetID(Item.GetID());
m_PotionBag[Slot].SetInventorySprite(Item.GetInventorySprite());
m_PotionBag[Slot].SetName(Item.GetName());
m_PotionBag[Slot].SetRestoreValue(Item.GetRestoreValue());
m_PotionCount[Slot] ++;							// and add one to the slots counter
return true;
}
else{
return false;
}

}

bool Item_Present = false;		//if item is already in the bag
bool Item_Fits = false;			//if the item is already in the bag and will fit into a stack
bool Empty_Space = false;
int Situation = 4;				// a value not in the enumeration(and thus not in the switch-case)
// enum types
// P_SIS  the item is present and a is has space in its stack
// NP_ES item is not already present and there is an empty space where it can go
// ALL_FULL there is no space for the item/ if it is present, all stacks are full
//
enum { P_SIS,NP_ES,ALL_FULL};

for(int i = 0;i < m_DropSlots;i++){				//checks to see if item is already present in the bag
if(m_DropBag.GetID() == Item.GetID()){
Item_Present = true;
}
}
for(int i = 0;i < m_DropSlots;i++){				//checks to see if there are any empty spaces
if(m_DropCount == 0){
Empty_Space = true;
}
}

if(Item_Present){									//if item is present then see if a stack has space
for(int i = 0;i < m_DropSlots;i++){
if(m_DropBag.GetID() == Item.GetID() && m_DropCount< DROP_STACK_LIMIT){	// checks to see if the stacks of the items are full
Item_Fits = true;
}
}
}

if(Item_Present && Item_Fits){					//if there is a space in a stack of the item already
Situation = P_SIS;
}else if(Empty_Space){							//if item is not already there or item is there but full then check for empty spaces
Situation = NP_ES;
}else{											// otherwise item does not fit in bag
Situation = ALL_FULL;
}

switch(Situation){
case P_SIS:
for(int i = 0;i < m_DropSlots;i++){
if(m_DropBag.GetID() == Item.GetID()){
m_DropCount ++;
return true;
break;
}
}
break;
case NP_ES:
for(int i = 0;i< m_DropSlots;i++){
if(m_DropCount == 0){					// if the block is empty
m_DropBag.SetDesc(Item.GetDesc());			//then copy the item into the space
m_DropBag.SetDropSprite(Item.GetDropSprite());
m_DropBag.SetID(Item.GetID());
m_DropBag.SetInventorySprite(Item.GetInventorySprite());
m_DropBag.SetName(Item.GetName());
m_DropBag.SetLevel(Item.GetLevel());
m_DropCount ++;							// and add one to the slots counter
return true;
break;
}
}
break;
case ALL_FULL:
// INSERT ERROR MESSAGE HERE
return false;
break;
default:
// INSERT UNEXPECTED ERROR MESSAGE HERE
return false;
break;
}
return false;
}
if(m_DropBag[Slot].GetID() == Item.GetID() && m_DropCount[Slot] < DROP_STACK_LIMIT){
m_DropCount[Slot] ++;
return true;
}
else if(m_DropCount[Slot] == 0){
m_DropBag[Slot].SetDesc(Item.GetDesc());			//then copy the item into the space
m_DropBag[Slot].SetDropSprite(Item.GetDropSprite());
m_DropBag[Slot].SetID(Item.GetID());
m_DropBag[Slot].SetInventorySprite(Item.GetInventorySprite());
m_DropBag[Slot].SetName(Item.GetName());
m_DropBag[Slot].SetLevel(Item.GetLevel());
m_DropCount[Slot] ++;							// and add one to the slots counter
return true;
}
else{
return false;
}

}
bool Item_Present = false;		//if item is already in the bag
bool Item_Fits = false;			//if the item is already in the bag and will fit into a stack
bool Empty_Space = false;
int Situation = 4;				// a value not in the enumeration(and thus not in the switch-case)
// enum types
// P_SIS  the item is present and a is has space in its stack
// NP_ES item is not already present and there is an empty space where it can go
// ALL_FULL there is no space for the item/ if it is present, all stacks are full
//
enum { P_SIS,NP_ES,ALL_FULL};

for(int i = 0;i < m_BaseSlots;i++){				//checks to see if item is already present in the bag
if(m_BaseBag.GetID() == Item.GetID()){
Item_Present = true;
}
}
for(int i = 0;i < m_BaseSlots;i++){				//checks to see if there are any empty spaces
if(m_BaseCount == 0){
Empty_Space = true;
}
}

if(Item_Present){									//if item is present then see if a stack has space
for(int i = 0;i < m_BaseSlots;i++){
if(m_BaseBag.GetID() == Item.GetID() && m_BaseCount< BASE_STACK_LIMIT){	// checks to see if the stacks of the items are full
Item_Fits = true;
}
}
}

if(Item_Present && Item_Fits){					//if there is a space in a stack of the item already
Situation = P_SIS;
}else if(Empty_Space){							//if item is not already there or item is there but full then check for empty spaces
Situation = NP_ES;
}else{											// otherwise item does not fit in bag
Situation = ALL_FULL;
}

switch(Situation){
case P_SIS:
for(int i = 0;i < m_BaseSlots;i++){
if(m_BaseBag.GetID() == Item.GetID()){
m_BaseCount ++;
return true;
break;
}
}
break;
case NP_ES:
for(int i = 0;i< m_BaseSlots;i++){
if(m_BaseCount == 0){					// if the block is empty
m_BaseBag.SetDesc(Item.GetDesc());			//then copy the item into the space
m_BaseBag.SetDropSprite(Item.GetDropSprite());
m_BaseBag.SetID(Item.GetID());
m_BaseBag.SetInventorySprite(Item.GetInventorySprite());
m_BaseBag.SetName(Item.GetName());
m_BaseBag.SetLevel(Item.GetLevel());
m_BaseBag.SetType(Item.GetType());
m_BaseCount ++;							// and add one to the slots counter
return true;
break;
}
}
break;
case ALL_FULL:
// INSERT ERROR MESSAGE HERE
return false;
break;
default:
// INSERT UNEXPECTED ERROR MESSAGE HERE
return false;
break;
}
return false;
}
if(m_BaseBag[Slot].GetID() == Item.GetID() && m_BaseCount[Slot] < BASE_STACK_LIMIT){
m_BaseCount[Slot] ++;
return true;
}
else if(m_BaseCount[Slot] == 0){
m_BaseBag[Slot].SetDesc(Item.GetDesc());			//then copy the item into the space
m_BaseBag[Slot].SetDropSprite(Item.GetDropSprite());
m_BaseBag[Slot].SetID(Item.GetID());
m_BaseBag[Slot].SetInventorySprite(Item.GetInventorySprite());
m_BaseBag[Slot].SetName(Item.GetName());
m_BaseBag[Slot].SetLevel(Item.GetLevel());
m_BaseBag[Slot].SetType(Item.GetType());
m_BaseCount[Slot] ++;							// and add one to the slots counter
return true;
}
else{
return false;
}
}
bool Item_Present = false;		//if item is already in the bag
bool Item_Fits = false;			//if the item is already in the bag and will fit into a stack
bool Empty_Space = false;
int Situation = 4;				// a value not in the enumeration(and thus not in the switch-case)
// enum types
// P_SIS  the item is present and a is has space in its stack
// NP_ES item is not already present and there is an empty space where it can go
// ALL_FULL there is no space for the item/ if it is present, all stacks are full
//
enum { P_SIS,NP_ES,ALL_FULL};

for(int i = 0;i < m_UpgradeSlots;i++){				//checks to see if item is already present in the bag
Item_Present = true;
}
}
for(int i = 0;i < m_UpgradeSlots;i++){				//checks to see if there are any empty spaces
Empty_Space = true;
}
}

if(Item_Present){									//if item is present then see if a stack has space
for(int i = 0;i < m_UpgradeSlots;i++){
if(m_UpgradeBag.GetID() == Item.GetID() && m_UpgradeCount< UPGRADE_STACK_LIMIT){	// checks to see if the stacks of the items are full
Item_Fits = true;
}
}
}

if(Item_Present && Item_Fits){					//if there is a space in a stack of the item already
Situation = P_SIS;
}else if(Empty_Space){							//if item is not already there or item is there but full then check for empty spaces
Situation = NP_ES;
}else{											// otherwise item does not fit in bag
Situation = ALL_FULL;
}

switch(Situation){
case P_SIS:
for(int i = 0;i < m_UpgradeSlots;i++){
return true;
}
}
break;
case NP_ES:
if(m_UpgradeCount == 0){					// if the block is empty
m_UpgradeBag.SetDesc(Item.GetDesc());			//then copy the item into the space
return true;
}
}
break;
case ALL_FULL:
// INSERT ERROR MESSAGE HERE
return false;
break;
default:
// INSERT UNEXPECTED ERROR MESSAGE HERE
return false;
break;
}
return false;
}
return true;
}
m_UpgradeBag[Slot].SetDesc(Item.GetDesc());			//then copy the item into the space
return true;
}
else{
return false;
}

}
bool Item_Present = false;		//if item is already in the bag
bool Item_Fits = false;			//if the item is already in the bag and will fit into a stack
bool Empty_Space = false;
int Situation = 4;				// a value not in the enumeration(and thus not in the switch-case)
// enum types
// P_SIS  the item is present and a is has space in its stack
// NP_ES item is not already present and there is an empty space where it can go
// ALL_FULL there is no space for the item/ if it is present, all stacks are full
//
enum { P_SIS,NP_ES,ALL_FULL};

for(int i = 0;i < m_WeaponSlots;i++){				//checks to see if item is already present in the bag
if(m_WeaponBag.GetID() == Item.GetID()){
Item_Present = true;
}
}
for(int i = 0;i < m_WeaponSlots;i++){				//checks to see if there are any empty spaces
if(m_WeaponCount == 0){
Empty_Space = true;
}
}

if(Item_Present){									//if item is present then see if a stack has space
for(int i = 0;i < m_WeaponSlots;i++){
if(m_WeaponBag.GetID() == Item.GetID() && m_WeaponCount< WEAPON_STACK_LIMIT){	// checks to see if the stacks of the items are full
Item_Fits = true;
}
}
}

if(Item_Present && Item_Fits){					//if there is a space in a stack of the item already
Situation = P_SIS;
}else if(Empty_Space){							//if item is not already there or item is there but full then check for empty spaces
Situation = NP_ES;
}else{											// otherwise item does not fit in bag
Situation = ALL_FULL;
}

switch(Situation){
case P_SIS:
for(int i = 0;i < m_WeaponSlots;i++){
if(m_WeaponBag.GetID() == Item.GetID()){
m_WeaponCount ++;
return true;
break;
}
}
break;
case NP_ES:
for(int i = 0;i< m_WeaponSlots;i++){
if(m_WeaponCount == 0){					// if the block is empty
m_WeaponBag.SetDesc(Item.GetDesc());			//then copy the item into the space
m_WeaponBag.SetDropSprite(Item.GetDropSprite());
m_WeaponBag.SetID(Item.GetID());
m_WeaponBag.SetInventorySprite(Item.GetInventorySprite());
m_WeaponBag.SetName(Item.GetName());
m_WeaponBag.SetAtk(Item.GetAtk());
m_WeaponBag.SetElement(Item.GetElement());
m_WeaponBag.SetPercentage(Item.GetPercentage());
m_WeaponCount ++;							// and add one to the slots counter
return true;
break;
}
}
break;
case ALL_FULL:
// INSERT ERROR MESSAGE HERE
return false;
break;
default:
// INSERT UNEXPECTED ERROR MESSAGE HERE
return false;
break;
}
return false;
}
if(m_WeaponBag[Slot].GetID() == Item.GetID() && m_WeaponCount[Slot] < WEAPON_STACK_LIMIT){
m_WeaponCount[Slot] ++;
return true;
}
else if(m_WeaponCount[Slot] == 0){
m_WeaponBag[Slot].SetDesc(Item.GetDesc());			//then copy the item into the space
m_WeaponBag[Slot].SetDropSprite(Item.GetDropSprite());
m_WeaponBag[Slot].SetID(Item.GetID());
m_WeaponBag[Slot].SetInventorySprite(Item.GetInventorySprite());
m_WeaponBag[Slot].SetName(Item.GetName());
m_WeaponBag[Slot].SetAtk(Item.GetAtk());
m_WeaponBag[Slot].SetElement(Item.GetElement());
m_WeaponBag[Slot].SetPercentage(Item.GetPercentage());
m_WeaponCount[Slot] ++;							// and add one to the slots counter
return true;
}
else{
return false;
}

}
bool Item_Present = false;		//if item is already in the bag
bool Item_Fits = false;			//if the item is already in the bag and will fit into a stack
bool Empty_Space = false;
int Situation = 4;				// a value not in the enumeration(and thus not in the switch-case)
// enum types
// P_SIS  the item is present and a is has space in its stack
// NP_ES item is not already present and there is an empty space where it can go
// ALL_FULL there is no space for the item/ if it is present, all stacks are full
//
enum { P_SIS,NP_ES,ALL_FULL};

for(int i = 0;i < m_ArmourSlots;i++){				//checks to see if item is already present in the bag
if(m_ArmourBag.GetID() == Item.GetID()){
Item_Present = true;
}
}
for(int i = 0;i < m_ArmourSlots;i++){				//checks to see if there are any empty spaces
if(m_ArmourCount == 0){
Empty_Space = true;
}
}

if(Item_Present){									//if item is present then see if a stack has space
for(int i = 0;i < m_ArmourSlots;i++){
if(m_ArmourBag.GetID() == Item.GetID() && m_ArmourCount< ARMOUR_STACK_LIMIT){	// checks to see if the stacks of the items are full
Item_Fits = true;
}
}
}

if(Item_Present && Item_Fits){					//if there is a space in a stack of the item already
Situation = P_SIS;
}else if(Empty_Space){							//if item is not already there or item is there but full then check for empty spaces
Situation = NP_ES;
}else{											// otherwise item does not fit in bag
Situation = ALL_FULL;
}

switch(Situation){
case P_SIS:
for(int i = 0;i < m_ArmourSlots;i++){
if(m_ArmourBag.GetID() == Item.GetID()){
m_ArmourCount ++;
return true;
break;
}
}
break;
case NP_ES:
for(int i = 0;i< m_ArmourSlots;i++){
if(m_ArmourCount == 0){					// if the block is empty
m_ArmourBag.SetDesc(Item.GetDesc());			//then copy the item into the space
m_ArmourBag.SetDropSprite(Item.GetDropSprite());
m_ArmourBag.SetID(Item.GetID());
m_ArmourBag.SetInventorySprite(Item.GetInventorySprite());
m_ArmourBag.SetName(Item.GetName());
m_ArmourBag.SetDef(Item.GetDef());
m_ArmourBag.SetElement(Item.GetElement());
m_ArmourBag.SetPercentage(Item.GetPercentage());
m_ArmourCount ++;							// and add one to the slots counter
return true;
break;
}
}
break;
case ALL_FULL:
// INSERT ERROR MESSAGE HERE
return false;
break;
default:
// INSERT UNEXPECTED ERROR MESSAGE HERE
return false;
break;
}
return false;
}
if(m_ArmourBag[Slot].GetID() == Item.GetID() && m_ArmourCount[Slot] < ARMOUR_STACK_LIMIT){
m_ArmourCount[Slot] ++;
return true;
}
else if(m_ArmourCount[Slot] == 0){
m_ArmourBag[Slot].SetDesc(Item.GetDesc());			//then copy the item into the space
m_ArmourBag[Slot].SetDropSprite(Item.GetDropSprite());
m_ArmourBag[Slot].SetID(Item.GetID());
m_ArmourBag[Slot].SetInventorySprite(Item.GetInventorySprite());
m_ArmourBag[Slot].SetName(Item.GetName());
m_ArmourBag[Slot].SetDef(Item.GetDef());
m_ArmourBag[Slot].SetElement(Item.GetElement());
m_ArmourBag[Slot].SetPercentage(Item.GetPercentage());
m_ArmourCount[Slot] ++;							// and add one to the slots counter
return true;
}
else{
return false;
}
}

void Inventory::DestroyItem(int Slot){}
void Inventory::MoveItem(int RootSlot, int TargetSlot){}



##### Share on other sites
I'm not sure where the problem is (maybe more info will help), but I can give you one suggestion - use an std::vector instead of those dynamic arrays, it can make your life much easier.

##### Share on other sites

i have briefly looked over std::vector and it is a lot more powerful, but i have zero experience with it. usually i would just study it and add it in but i am on a tight deadline for this project and dont have time to be adding things i dont know.

but will definately look into vectors for future use.

what sorts of extra information were you looking for?

the way it works is that the player class is instanced and an Inventory class is a public data member

class Inventory{private:Item_Potion *m_PotionBagpublic:Inventory();                       // as shown before it tries to dynamically                                             // allocate a number of "spaces" in each bagAddToPotion(Item_Potion);          //adds an potion item to the potion bag                                   // checks to see if there is room etc};class Player{private:public:Player();Inventory *m_Inventory            //allocated in constructir}

thats the basic relationship between player and inventory

so you can add an item (stored seperatly) to the players potion bag by using the call

if there is any specific i havent already covered just ask and i'll try my best to explain

##### Share on other sites
Your problem is that new only reserves memory for you. You need to create your objects too, like this:
p_CustomClass = new CustomClass[m_DynamicNumber];for(int i = 0; i < number_of_objects; ++i){    p_CustomClass = new CustomClass();}

But as Gage64 said, you'd probably be much better of with a std::vector.

##### Share on other sites
Quote:
 Perost wrote:Your problem is that new only reserves memory for you. You need to create your objects too...

Thats's incorrect, new allocates memory and calls each object's c'tor - note that he's allocating class objects, not pointers to objects.

The problem is that when your allocating these arrays, the default c'tor gets called for each object. In your Item classes the default c'tors are all empty so they don't do any initialization. Your comments say they shouldn't be used so just remove them (this will break your code but it's incorrect anyway).

##### Share on other sites
Quote:
 The problem is that when your allocating these arrays, the default c'tor gets called for each object. In your Item classes the default c'tors are all empty so they don't do any initialization. Your comments say they shouldn't be used so just remove them (this will break your code but it's incorrect anyway).

if that were the case i would still be able to access the public functions of an item say : m_PotionBag[1].Use();

(if there were a use function)

but as it is, all i can do is access m_PotionBag[0], calls to any other member of the array just doesnt do anything and it isnt showing up in msvc intellisense
(although i dont take this as gospel it is usually a good indicator)

Note: the notes about not using the default constructors for the items is just a footnote to me as i wanted all initialisation to be done explicitly if needed
(or better yet make it so that no initialization is needed)

@Perost just trying the individual allocation thing now (i thought the entire array's constructors were called by default) :)

Edit: Didnt work, as i said i cant access the individual members because as far as its concerned there is only 1 member

completely stupmped me now:(

do you think it might have something to do with me creating the pointer as a private member but not allocating it till the constructor?

##### Share on other sites
Oops, my mistake. A simple test shows that new does indeed call the default constructor. I haven't used raw dynamically located arrays in a very long time [smile]

Quote:
 but as it is, all i can do is access m_PotionBag[0], calls to any other member of the array just doesnt do anything and it isnt showing up in msvc intellisense(although i dont take this as gospel it is usually a good indicator)

What do you mean with this? If the program doesn't segfault, but instead doesn't do anything, then it's probably not a memory problem, but rather a logical error.

##### Share on other sites
A quick summary of what you need to do in the current case to use std::vector:

1) Include <vector>
2) Change this:
	int *m_PotionCount;	int *m_DropCount;	int *m_BaseCount;	int *m_UpgradeCount;	int *m_WeaponCount;	int *m_ArmourCount;	Item_Potion *m_PotionBag;	Item_MonsterDrop *m_DropBag;	Item_Base *m_BaseBag;	Item_Upgrade *m_UpgradeBag;	Item_Weapon *m_WeaponBag;	Item_Armour *m_ArmourBag;
to this:
	std::vector<int> m_PotionCount;	std::vector<int> m_DropCount;	std::vector<int> m_BaseCount;	std::vector<int> m_UpgradeCount;	std::vector<int> m_WeaponCount;	std::vector<int> m_ArmourCount;	std::vector<Item_Potion> m_PotionBag;	std::vector<Item_MonsterDrop> m_DropBag;	std::vector<Item_Base> m_BaseBag;	std::vector<Item_Upgrade> m_UpgradeBag;	std::vector<Item_Weapon> m_WeaponBag;	std::vector<Item_Armour> m_ArmourBag;

3) Change this:
	m_PotionBag = new Item_Potion[m_PotionSlots];	// assigns an array with amount of slots defined previously	m_PotionCount = new int[m_PotionSlots];			// and also creates an array that holds a counter for each slot	m_DropBag = new Item_MonsterDrop[m_DropSlots];	// so items can stack 	m_DropCount = new int[m_DropSlots];		m_BaseBag = new Item_Base[m_BaseSlots];	m_BaseCount = new int[m_BaseSlots];	m_UpgradeBag = new Item_Upgrade[m_UpgradeSlots];	m_UpgradeCount = new int[m_UpgradeSlots];	m_WeaponBag = new Item_Weapon[m_WeaponSlots];	m_WeaponCount = new int[m_WeaponSlots];	m_ArmourBag = new Item_Armour[m_ArmourSlots];	m_ArmourCount = new int[m_ArmourSlots];
To this:
	m_PotionBag.resize(m_PotionSlots);	m_PotionCount.resize(m_PotionSlots);	m_DropBag.resize(m_DropSlots);	m_DropCount.resize(m_DropSlots);		m_BaseBag.resize(m_BaseSlots);	m_BaseCount.resize(m_BaseSlots);	m_UpgradeBag.resize(m_UpgradeSlots);	m_UpgradeCount.resize(m_UpgradeSlots);	m_WeaponBag.resize(m_WeaponSlots);	m_WeaponCount.resize(m_WeaponSlots);	m_ArmourBag.resize(m_ArmourSlots);	m_ArmourCount.resize(m_ArmourSlots);

4) Remove this:
	delete []m_PotionBag;		delete []m_PotionCount;	delete []m_DropBag;	delete []m_DropCount;		delete []m_BaseBag;	delete []m_BaseCount;	delete []m_UpgradeBag;	delete []m_UpgradeCount;	delete []m_WeaponBag;	delete []m_WeaponCount;	delete []m_ArmourBag;	delete []m_ArmourCount;

All array access is handled exactly the same way as with a raw array, using square brackets ([]). Memory that is allocated by the vector (during the .resize() call, for example) is automatically cleaned up for you when the vector goes out of scope (in this case, when the class is destructed), and destructors are called on all the elements in the vector that were constructed. Also, you can always find the size of the vector just by calling the .size() member function, so you don't really need to store a separate variable to indicate the size of the array anymore. In fact, it might be better to avoid that, since when the vector changes size, its .size() function automatically returns the new size, bur your external size variable won't be correct until you fix it manually.

I don't know what your actual problem is, but this may somehow avoid it, and it's a good change to make anyway.

##### Share on other sites
I second (Or third, or whatever count we're on now) the suggestion to use vectors. They're a nice way to contain arrays, and most STL implementations will even catch out of bounds errors (Accessing element index -1, or past the end of the array), which you wouldn't get with a standard array.

##### Share on other sites
ok you've persuaded me i'll try std::vector see if its not to hard to implement

and thx to you all

• 36
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631359
• Total Posts
2999545
×