Random monster generator txt RPG?

hey im working on a random monster generator and im getting a lil problem with my GetName() function and list something about it not being able to access class ENTITY.. ill post the code and the actual error if someone can help it would be greatly appreciated
i also havent set up the constructos yet idk if thats part of the problem or not
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//entity.h
class ENTITY
{
private:

	char name[32];
	int gold;
	int exp;
	int health;
	int maxhealth;
	int mana;
	int maxmana;

public:	

	bool SetName(char* newname);
	char* GetName();
	void SetGold(int newgold);
	void AddGold(int amount);
	void SpendGold(int amount);
	int GetGold();
	void SetExp(int newexp);
	void AddExp(int amount);
	int GetExp();
	void SetHealth(int newhealth);
	void AddHealth(int amount);
	void LoseHealth(int amount);
	int GetHealth();
	void SetMaxHealth(int newmaxhealth);
	int GetMaxHealth();
	void SetMana(int newmana);
	void AddMana(int amount);
	void LoseMana(int amount);
	int GetMana();
	void SetMaxMana(int newmana);
	int GetMaxMana();
};


Entity.cpp
there are more functions in this file but these 2 are relevant to the problem im having
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

bool ENTITY::SetName(char *newname)
{
	if(strlen(newname) == 0)
		return false;
	
	if(strlen(newname) > 32)
		return false;

	strcpy(name,newname);
	return true;

char* ENTITY::GetName()
{
	return name;
}
}


battle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

// BATTLE

#include "Library.h"

bool Battle(PLAYER* player,MONSTER* list,int count)
{
	while (player->GetHealth() > 0)
	{
		// player goes first
		cout << "You Clecnh Your " << player->GetWeapon()->GetName() << "tightly and face the enemy.\n\n";
		
		//display enemy count
		if (count == 1)
		{
			cout << "The" << list->GetName() << "stares at you, ready to attack.\n";
			
			Wait();
		}
		else
		{
			cout << "Enemies\n";
			for (int loopname = 0; loopname < count; loopname++)
			{
				if(list[loopname].GetHealth() > 0)
				{
					cout << list->name[loopname].GetName();

					// output status effect
					// goblin - poisoned
				}
			}
			
		Wait();
	}
		// display player stats
		cout << player->GetName() << endl;
		//health / maxhealth. mana /maxmana
		cout << "Health" << player->GetHealth() << " / " << player->GetMaxHealth() << endl;
		cout<< "Mana" << player->GetMana() << " / " << player->GetMaxMana() << endl;

		cout << "1: Attack\n";
		cout << "2: Spell\n";
		cout << "3: Item\n";
		//
		

		//action?

		//check action

		// is enemy dead?

		//enemies turn

		if (count == 1)
		{
			// have enemy fight
		}
		else
		{
			// loop
			for (int loop = 0; loop < count; loop++)
			{
				// list[loop]
			}
		}
		
	}

	cout << " Thou hast been destroyed.\n\n";

	return false; // you died
}


i also have a library header which has my monster.h/cpp and my player.h/cpp here are the errors

im doing a tutorial on making it but the error didnt happen to the person in the tutorial...

i know this is alot to read... here's the errors.....

5 IntelliSense: member "ENTITY::name" (declared at line 5 of "c:\users\x d y n a s 7 y\desktop\afternoon\runes of sarnac 2.0\runes of sarnac 2.0\Entity.h") is inaccessible c:\users\x d y n a s 7 y\desktop\afternoon\runes of sarnac 2.0\runes of sarnac 2.0\battle.cpp 26


4 IntelliSense: expression must have class type c:\users\x d y n a s 7 y\desktop\afternoon\runes of sarnac 2.0\runes of sarnac 2.0\battle.cpp 26



Error 2 error C2248: 'ENTITY::name' : cannot access private member declared in class 'ENTITY' c:\users\x d y n a s 7 y\desktop\afternoon\runes of sarnac 2.0\runes of sarnac 2.0\battle.cpp 26
1) What line has the error?

2) Refrain from using char arrays. You allow a name length of 32, but a name length of 32 would corrupt your memory. Any reason you don't want to use the much easier/safer std::string?

3) Get/Set hell!

EDIT:

oh wait ... the line number is in the error XD.

cout << list->name[loopname].GetName();

Was this supposed to be list[loopname].GetName(); ??
Last edited on
yes that worked
thank you

i did char array cuz the tutorial put it in like that
i dont want to corrupt memory though :S

if(strlen(newname) > 32)
return false;
if i change this to >= 32

would that prevent memory corruption?
Last edited on
i did char array cuz the tutorial put it in like that


Then I'd argue you're using a bad tutorial. ;P

But really, tutorials are just a guide. You can make improvements where necessary. This is one of those cases.

std::string is your friend. Use it.

would that prevent memory corruption?


The fact you can't answer this on your own is exactly why you shouldn't be using char arrays. char arrays are dangerous unless you understand exactly how they work (and even then they're risky because it's easy to slip up).

But to answer your question, yes, that would prevent it.
i agree i shouldnt be using something that i dont fully understand in general whether its dangerous or not

and i appreciate all your help man

thanks
Topic archived. No new replies allowed.