Wierd little thing when assigning class values

So, I'm writing a roguelike game in C++. But, its complaining about a class I made called "Player" in the file "player.h" in the file "monster". When I input into monster this function:
1
2
3
4
5
6
7
	void attackplayer(Player hero, bool checkup, bool checkdown, bool checkleft, bool checkright)
	{
		if (checkup == true || checkdown == true || checkleft == true || checkright == true)
		{
			hero.damage(damage);
		}
	}

with player.h included, it complains hero is of an error type, even though it is initialized as a player. How do I fix this? It used to complain it couldn't find the Player Identifier, but it seemed to fix itself on that.
It is hard to say without seeing a more complete example. You are copying by value which I believe is incorrect. If you want the original hero object to be modified then you need to pass by reference. Is the Player class copyable? What is the exact error message and can you provide more detail such as a small example or the Player class declaration or the complete file that contains the attackPlayer function?
1
2
// I think that this is the declaration that you want.
void attackplayer(Player& hero, bool checkup, bool checkdown, bool checkleft, bool checkright);
I did what you did. However, when I mouse over hero is still of an error type. The full Monster.h Source is here:
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef MONSTER_H
#define MONSTER_H

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include "Player.h"

class Monster
{
private:
	int xpgiven;
	int damage;
	int maxhp;
	int curhp;
	int defense;
	sf::Font MonsterFont;
	sf::String MonsterSprite;
	float height;
	float width;
	float ypos;
	float xpos;
	bool alive;
public:
	Monster(sf::Unicode::Text sprite, int damage, int maxhp, int xpgiven, int defense, float xpos, float ypos, bool running)
	{
		xpgiven = xpgiven;
		damage = damage;
		maxhp = maxhp;
		curhp = maxhp;
		defense = defense;
		if (!MonsterFont.LoadFromFile("Lucida Console.ttf", 12))
		{
			running = false;
		}
		MonsterSprite.SetFont(MonsterFont);
		MonsterSprite.SetSize(12);
		MonsterSprite.SetText(sprite);
		MonsterSprite.SetColor(sf::Color(192, 192, 192));
		MonsterSprite.SetPosition(xpos, ypos);
		height = 20.0;
		width = 13.0;
		xpos = xpos;
		ypos = ypos;
		alive = true;
	}
	int getxpgiven() {return xpgiven;}
	int getdamage() {return damage;}
	int getmaxhp() {return maxhp;}
	int getcurhp() {return curhp;}
	float getxpos() {return xpos;}
	float getypos() {return ypos;}
	float getheight() {return height;}
	float getwidth() {return width;}
	bool checklife() {return alive;}
	void attackplayer(Player& hero, bool checkup, bool checkdown, bool checkleft, bool checkright)
	{
		if (checkup == true || checkdown == true || checkleft == true || checkright == true)
		{
			hero.damage(damage);
		}
	}
	void statcheck()
	{
		if (curhp > maxhp)
		{
			curhp = maxhp;
		}
	}
	bool death()
	{
		if (curhp <= 0)
		{
			MonsterSprite.SetText(" ");
			alive = false;
			return true;
		}
	}
	void takedamage(int nValue)
	{
		curhp -= nValue - defense;
		death();
		statcheck();
	}
	void heal(int nValue)
	{
		curhp += nValue;
		death();
		statcheck();
	}
	void damagereduction(int nValue)
	{
		damage -= nValue;
	}
};

#endif 


And here is the build message:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1>  Simple Rogue
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(56): error C2061: syntax error : identifier 'Player'
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): error C2065: 'checkup' : undeclared identifier
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): warning C4805: '==' : unsafe mix of type ''unknown-type'' and type 'bool' in operation
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): error C2065: 'checkdown' : undeclared identifier
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): warning C4805: '==' : unsafe mix of type ''unknown-type'' and type 'bool' in operation
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): error C2065: 'checkleft' : undeclared identifier
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): warning C4805: '==' : unsafe mix of type ''unknown-type'' and type 'bool' in operation
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): error C2065: 'checkright' : undeclared identifier
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(58): warning C4805: '==' : unsafe mix of type ''unknown-type'' and type 'bool' in operation
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(60): error C2065: 'hero' : undeclared identifier
1>c:\users\gio\desktop\documents\c++ programs\2d & 3d games\roguelike game\roguelike game\monster.h(60): error C2228: left of '.damage' must have class/struct/union
1>          type is ''unknown-type''
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

They all seem to be related to the problem.
Hrm, are you sure you didn't misspell Player in your .h file?
Yes, all of them are spelled Player, and when I run the mouse over the Player identifier, it syas "Class Player".
Could you post Player.h?
Topic archived. No new replies allowed.