Can't figure out this problem

Started by
3 comments, last by heidiann 13 years, 4 months ago
I have tried and tried again to figure this out, but I am at a loss and hoping to receive some help here at gamedev. When I run in debug it seems to point to

Item::GetName() function, my code you can see below.

#include <iostream>#include <string>using namespace std;//Generic item classclass Item{public:	Item()	{		name = "";		price = 0;	}	void SetName(string newName)	{		name = newName;	}	void SetPrice(int amount)	{		price = amount;	}	string GetName()	{		return name;	}	int GetPrice()	{		return price;	}private:	std::string name;	int price;};//Consumable item derived from generic classclass Potion : public Item{public:	Potion()	{		quantity = 0;	}	Potion(Potion* newPotion)	{		CreatePotion(newPotion->GetName(), newPotion->GetPrice());		quantity = 1;	}	void CreatePotion(string name, int cost)	{		SetName(name);		SetPrice(cost);	}	void SetQuantity(int amount)	{		quantity = amount;	}	void AddQuantity(int amount)	{		quantity += amount;	}	int GetQuantity()	{		return quantity;	}private:	int quantity;};class Entity{public:	void SetName(string newName)	{		name = newName;	}	string GetName()	{		return name;	}private:	string name;};class Player : public Entity{public:	Player()	{		//do nothing	}	Player(string name)	{		SetName(name);	}	void AddPotion(Potion* potion)	{		if (potion == NULL)			return;		//check for duplicate		for (int check = 0; check < 15; check++)		{			if (potionList[check] != NULL)			{				if (potionList[check]->GetName() == potion->GetName())				{					//names are same					potionList[check]->AddQuantity(1);					return;				}			}		}		//no duplicates found		for (int loop=0;loop < 15; loop++)		{			if (potionList[loop] == NULL)			{				//create a potion here				potionList[loop] = new Potion(potion);				return;			}		}		cout << "There is no room in your inventory.\n";		cout << endl;	}	Potion** GetPotionList()	{		return potionList;	}	void DisplayPotions()	{		//list potions		cout << "POTIONS\n\n";		Potion** potionlist = GetPotionList();		for (int loop=0;loop < 15;loop++)		{			if (potionlist[loop] != NULL)			{				cout << potionlist[loop]->GetQuantity() << " x " << potionlist[loop]->GetName() << endl;			}		}	}private:	Potion* potionList[15];};int main(){	bool isRunning = true;	Player* Hero = new Player("Heidi");	Potion* PotionList = new Potion[2];	cout << "Name: " << Hero->GetName() << endl;	PotionList[0].CreatePotion("HP", 10);	PotionList[1].CreatePotion("MP", 5);	for(int i = 0; i < 2; i++)	{		cout << "Name: " << PotionList.GetName() << " - Cost: " << PotionList.GetPrice() << endl;	}	Hero->AddPotion(&PotionList[0]);	Hero->AddPotion(&PotionList[1]);	return 0;}
Advertisement
You don't mention what problem you're having.

However, just in case, you might try:
cout << "Name: " << Hero->GetName().c_str() << endl;

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thx for the quick response, what is happening wrong is the program completely bombs out. It doesn't bomb till I call

Hero->AddPotion(&PotionList[0]);
Hero->AddPotion(&PotionList[1]);

In debug mode, it points to Item::GetName()
In your AddPotion function:
potionList[check] is never going to == NULL, since you never cleared your potionList[15] to all NULL pointers. Right now they are filled with garbage.
if( garbage != NULL ) is ok, so you then procede to garbage->GetName()

In the constructor, you need to NULL out all the members of that array.
Wow, what an oversight that was. I have to remember to initialize all variables in the future;

This topic is closed to new replies.

Advertisement